Top 20 Java Collections Framework Interview Questions (With Detailed Answers)
Q1. What is the Java Collections Framework?
Answer:
It’s a unified architecture to manage and manipulate groups of data. It includes interfaces (like List, Set), classes (ArrayList, HashMap) and utility classes (Collections, Arrays). It simplifies development and promotes reusability.
Q2. Difference between List, Set, and Map?
Answer:
- List: Ordered, allows duplicates
- Set: No duplicates
- Map: Key-value pairs
Used based on the need for ordering, uniqueness, or key-based access.
Q3. Difference between ArrayList and LinkedList?
Answer:
- ArrayList is backed by array, fast read, slow insert/delete.
- LinkedList is backed by doubly linked nodes, slow read, fast insert/delete.
Q4. Difference between HashSet and TreeSet?
Answer:
- HashSet: Unordered, fast operations using hash table
- TreeSet: Sorted, slower operations using Red-Black Tree
Q5. How does HashMap work internally?
Answer:
Uses hashcode and equals of keys. Stores entries in a bucket array. Collisions are handled using LinkedList or Tree in Java 8+. Bucket index = hash(key) % capacity.
Q6. Difference between HashMap and Hashtable?
Answer:
- HashMap: Not synchronized, allows null
- Hashtable: Synchronized, no nulls
HashMap is preferred for most applications.
Q7. What is ConcurrentHashMap?
Answer:
Thread-safe version of HashMap. Uses segment or bucket-level locking to allow concurrent read/write.
Q8. Difference between TreeMap and HashMap?
Answer:
- TreeMap: Sorted, uses Red-Black Tree
- HashMap: Unordered, uses hash table
TreeMap is slower but good when sorted data is needed.
Q9. What is the role of equals() and hashCode() in HashMap?
Answer:
They ensure uniqueness and correct placement/retrieval of keys. Bad implementations can lead to key collisions or data loss.
Q10. What are fail-fast and fail-safe iterators?
Answer:
- Fail-fast: Throws ConcurrentModificationException (like ArrayList)
- Fail-safe: Works on clone/snapshot (like ConcurrentHashMap)
Q11. Difference between Iterator and ListIterator?
Answer:
- Iterator: Forward-only, supports remove
- ListIterator: Bidirectional, supports add, remove, set
Only List types support ListIterator.
Q12. Difference between Array and ArrayList?
Answer:
- Array: Fixed size, holds primitives/objects
- ArrayList: Dynamic, object-only, rich methods (add(), remove())
Q13. How does TreeSet maintain order?
Answer:
Uses Red-Black Tree. Elements must implement Comparable or be passed a Comparator.
Q14. Difference between LinkedHashMap and HashMap?
Answer:
- HashMap: No order
- LinkedHashMap: Maintains insertion order
Used when order-sensitive data is needed.
Q15. What is the use of Collections utility class?
Answer:
Contains static methods like sort(), reverse(), shuffle(), binarySearch(), synchronizedList(), etc.
Q16. Difference between Collection and Collections?
Answer:
- Collection: Interface
- Collections: Utility class with static helper methods
Q17. What is a PriorityQueue in Java?
Answer:
A queue where elements are ordered by priority. Internally uses a heap structure. Default order is natural; custom Comparator can be provided.
Q18. What is EnumSet in Java?
Answer:
A high-performance Set for enum types. Faster and memory-efficient compared to HashSet. All elements must be from same Enum type.
Q19. Difference between SynchronizedList and CopyOnWriteArrayList?
Answer:
- SynchronizedList: Wrapper over ArrayList with synchronized access
- CopyOnWriteArrayList: Thread-safe with copy-on-write mechanism. Better for read-heavy applications.
Q20. How to sort a HashMap by values?
Answer:
Convert to List<Map.Entry<K, V>>, then use Collections.sort() with custom Comparator. Or use Stream API for cleaner sorting.
Conclusion
Java Collections is the heart of backend interviews. These 20 questions cover everything from basics to concurrency. Practice these answers out loud, apply them in projects, and you'll ace your next interview.
0 Comments