Skip to main content

Java Collections Framework Interview Questions & Answers

These questions cover essential and tricky concepts of the Java Collections Framework as discussed in the Code Decode tutorial.

1. Explain the Collection Hierarchy

The hierarchy starts with the Collection interface, which is the root of the framework. Most collections inherit from this, except for the Map interface.

  • List: An ordered collection that allows duplicates and index-based access (e.g., ArrayList, LinkedList, Vector).
  • Set: A collection that does not allow duplicates and does not guarantee order (e.g., HashSet, LinkedHashSet, TreeSet).
  • Queue: Follows the FIFO (First-In, First-Out) principle (e.g., PriorityQueue, ArrayDeque).
  • Map: Represents key-value pairs. It does not extend the Collection interface but is part of the framework.

2. Why does Map not extend the Collection interface?

The Collection interface works with a single object (using the add(E e) method), whereas the Map interface works with key-value pairs (using the put(K key, V value) method). Because the basic operations and structure are fundamentally different, Map is kept as a separate hierarchy.

3. What is the difference between Fail-Fast and Fail-Safe Iterators?

  • Fail-Fast: Throws a ConcurrentModificationException immediately if the collection is structurally modified (e.g., adding or removing elements) while iterating. It works on the original collection.
  • Fail-Safe: Does not throw an exception because it works on a clone/copy of the collection rather than the original.

4. What is a BlockingQueue?

Found in the java.util.concurrent package, a BlockingQueue is a thread-safe queue. It "blocks" a thread in two specific scenarios:

  1. When a thread tries to dequeue from an empty queue (it waits until an element is available).
  2. When a thread tries to enqueue into a full queue (it waits until space becomes available).

5. Synchronized vs. Concurrent Collections

While both are thread-safe, their performance and internal mechanisms differ:

  • Synchronized Collections (e.g., Hashtable, Vector): Lock the entire collection for every operation. This leads to poor scalability as other threads must wait for the lock to be released.
  • Concurrent Collections (e.g., ConcurrentHashMap): Use lock stripping, where the collection is divided into segments. Only the specific segment being modified is locked, allowing multiple threads to perform operations on different segments simultaneously.

6. How does HashMap work internally?

HashMap works on the principle of hashing:

  1. Put Operation: When you call put(K, V), the hashCode() of the key is used to calculate an index (bucket location) in the internal array.
  2. Storage: It stores a Map.Entry object containing both the key and the value at that index.
  3. Collision Handling: If two different keys result in the same index, a linked list (or a balanced tree in newer Java versions) is created at that bucket to store multiple entries.
  4. Get Operation: When you call get(K), the hashCode() finds the bucket, and the equals() method is used to find the specific key within the linked list/tree to return the correct value.