Java Collections

2015/10/25

What are different ways to iterate over a list?

List<String> strList = new ArrayList<>();
//using for-each loop
for(String obj : strList){
    System.out.println(obj);
}
//using iterator
Iterator<String> it = strList.iterator();
while(it.hasNext()){
    String obj = it.next();
    System.out.println(obj);
}

Using iterator is more thread-safe because it makes sure that if underlying list elements are modified, it will throw ConcurrentModificationException

What do you understand by iterator fail-fast property?

Iterator fail-fast property checks for any modification in the structure of the underlying collection everytime we try to get the next element. If there are modifications found, it throws ConcurrentModificationException. All the implementations of Iterator in Collection classes are fail-fast by design except the concurrent collection classes like ConcurrentHashMap and CopyOnWriteArrayList.

How HashMap works?

HashMap stores key-value pair in Map.Entry static nested class implementation. HashMap works on hashing algorithm and use hashCode() and equals() method in put and get methods.

When we call put method by passing key-value pair, HashMap uses Key hashCode() with hashing to find out the index to store the key-value pair. The entry is stored in the LinkedList, so if there are already existing entry, it uses equals() method to check if the passed key already exists, if yes it overwrites the value, else it creates a new entry and store this key-value entry.

If o1.equals(o2), then o1.hashCode() == o2.hashCode() should always be true

If o1.hashCode() == o2.hashCode(), it doesn't mean that o1.equals(o2) will be true

What is difference between HashMap and HashTable?

  • HashMap allows null key and values whereas HashTable doesn't allow null key and values
  • HashTable is synchronized but HashMap is not synchronized. So HashMap is better for single threaded environment, HashTable is suitable for multi-threaded environment

How to decide between HashMap and TreeMap?

For inserting, deleting, and locating elements in a Map, the HashMap offers the best alternative. If, however, you need to traverse the keys in a sorted order, then TreeMap is your better alternative.

What is difference between ArrayList and LinkedList?

  • ArrayList is an index based data structure backed by Array, so it provides random access to its elements with performance as O(1) whereas performance in LinkedList is O(n)
  • Insertion, addition or removal of an element is faster in LinkedList compared to ArrayList because there is no concept of resizing array or updating index when element is added in middle
  • LinkedList consumes more memory than ArrayList because every node in LinkedList stores reference of previous and next elements

What is BlockingQueue?

java.util.concurrent.BlockingQueueis a Queue that supports operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.