Collection Framework Interview Questions – Set 2

Posted on Updated on

Hello Folks, welcome back to Collection Framework Interview Questions – Set 2. If you missed to visit the set 1 please click on below link.

Collection Framework Interview Questions Set 1

1). How to retrieve elements from Collections ?

There are following way to traverse/retrieve the elements from Collections

a). for-each loop for loop which repeatedly executes a group of statement for each element of the collection.

b). Iterator interface – It contains method to retrieve the elements one by one from a collection.

i). boolean  hasNext() : This method return true if the iterator has more elements.

ii). element next() :  This element return the next element in the iterator.

iii). void remove() : This method removes from the collection the last element returned by the iterator.

c). ListIterator interface – It contains methods  to retrieve the elements from collection object, both in forward and reverse direction. It has following important method.

i). boolean hasNext() : This return true if the ListIterator has more element when traversing the list in the   forward direction.

ii). boolean hasPrevious() : This returns true if the ListIterator has more element when traversing the list in the reverse direction.

iii). element next() : return next element in the list.

iv). Element previous() : returns the previous element in the list.

v). void remove() : This removes from the list the last element that was returned by the next() or previous() methods.

d). Enumeration Interface – Useful to retrieve one by one elements like the Iterator. It has two methods/

i). boolean hasMoreElements() : This method tests if the Enumeration has any more elements or not.

ii). Elemetn hasElement() : This return the next element that is available in Enumeration.


2). Why developer get a ConcurrentModificationException when using Iterator ?

The java.util Collection classes are fail-fast, which means that if one thread changes a collection while another thread is traversing it through with an Iterator the iterator.hasNext() or call with throw ConcurrentModifcationException.



3). How to avoid ConcurrentModificationException ?

For multi-thread access situation:

  1. Solution 1:  Convert your list to an array with list.toArray() and iterate on the array. This approach is not recommended if the list is large.
  2. Solution 2: You can lock the entire list while iterating by wrapping your code within a synchronized block. This approach adversely affects scalability of your application if it is highly concurrent.
  3. Solution 3: If you are using JDK 1.5 then you can use the ConcurrentHashMap and CopyOnWriteArrayList classes, which provide much better scalability and the iterator returned by ConcurrentHahMap.iterator() will not throw ConcurrentModificationException while preserving thread-safety.

For Single-thread access situation:

  1. Use it.remove() – remove the current object via the Iterator “it” which has a reference to your underlying collection.
  2. Avoid yourCollection.remove(yourObject) – avoid by passing the iterator. When is called, can throw the exception.         

4). Why Map interface does not extend Collection interface ?

Sometime interviewer asked such question to confuse or check the basic of the concepts. Well, the good answer to this question is that “because they are not compatible“. Collection has method add(Object o) while Map does not have such method because they need key-value pair. Other reason is that Map supports keySets, valueSets etc and Collection classes does not have such view.

5). What are different Collection views provided by Map interface ?

Map interface provide 3 views of key-value pairs stored in it.

  • key set view
  • value set view
  • entry set view

All the view can be navigated using Iterator.

6).  How will you make Collections readOnly ?

We can make Collection readOnly using following method –

  • Collection.unmodifiableList(list)
  • Collection.unmodifiableMao(map)
  • Collection.unmodifiableSet(set)

These methods take collection as parameter and return type of collection which are synchronized and thread safe.

7). How we can sort a list of Objects ?

  • Array.sort() – method to sort the array of the object.
  • Collection.sort() – method to sort the collection of object.

8). Can we add a NULL element to HashSet and TreeSet ?

We can add one null element to HashSet while TreeSet doesn not allow any null element.

9). What is IdentityHashMap ?


Class IdentityHashMap<K,V>

IdentityHashMap is a class present in java.util package. This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)

IdentityHashMap is not synchronized. Iterators returned by the iterator() method are fail-fast , hence , will throw ConcurrentModificationException.

You can learn more from here.

10). What is WeakHashMap ?


Class WeakHashMap<K,V>

WeakHashMap is a class present in java.util package similar to IdentityHashMap. Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector.

Both null values and the null key are supported. This class has performance characteristics similar to those of the HashMap class, and has the same efficiency parameters of initial capacity and load factor.

Like most collection classes, this class is not synchronized. A synchronized WeakHashMap may be constructed using the Collections.synchronizedMap method.

You can learn more from here.


Stay tuned for Collection Framework Interview Question – set 3.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.