Java Collections Interview Complete Guide
Q1. Difference between Collection and Collections?
Collection is an interface (root of the collection hierarchy).
Collections is a utility class that provides static methods like sort(), reverse(), etc.
Q2. Main interfaces in Java Collections Framework?
List, Set, Queue, Deque, and Map.
Q3. Difference between List, Set, and Map?
List → Ordered, allows duplicates.
Set → Unordered, no duplicates.
Map → Stores key-value pairs, unique keys.
Q4. ArrayList vs LinkedList?
ArrayList → Fast random access, slow insertion/deletion.
LinkedList → Fast insertion/deletion, slow random access.
Q5. HashSet vs TreeSet?
HashSet → Unordered, uses hashing (O(1)).
TreeSet → Sorted order, uses Red-Black Tree (O log n).
Q6. HashMap vs Hashtable?
HashMap → Non-synchronized, 1 null key allowed.
Hashtable → Synchronized, no null keys allowed.
Q7. Why Map is not part of Collection?
Because Collection works with single objects, while Map stores key-value pairs.
Q8. Array vs ArrayList?
Array → Fixed size, can store primitives.
ArrayList → Dynamic size, only objects.
Q9. Null values in Collections?
HashMap → 1 null key, many null values.
HashSet → 1 null element.
TreeMap → Null keys not allowed.
Q10. Fail-fast vs Fail-safe iterators?
Fail-fast (Iterator) → Throws ConcurrentModificationException.
Fail-safe (ConcurrentHashMap, CopyOnWriteArrayList) → Works on a clone.
Q11. How does HashMap work internally?
Uses hashCode() → bucket → equals() check. Collisions handled with chaining/tree.
Q12. Iterator vs ListIterator?
Iterator → Forward only, works on Collection.
ListIterator → Forward + backward, only for List.
Q13. Comparable vs Comparator?
Comparable → Natural order (`compareTo`).
Comparator → Custom order (`compare`).
Q14. How to synchronize a Collection?
Example:
`List list = Collections.synchronizedList(new ArrayList<>());`
Q15. ConcurrentHashMap vs Hashtable?
Hashtable → Locks whole map.
ConcurrentHashMap → Locks segments → better performance.
Q16. Sorting a List example:
import java.util.*; class SortExample { public static void main(String[] args) { List list =
Arrays.asList(5, 3, 1); Collections.sort(list); System.out.println(list); } }
Q17. ArrayDeque vs Stack?
ArrayDeque → Faster, non-synchronized.
Stack → Legacy, synchronized.
Q18. HashMap vs LinkedHashMap?
HashMap → No order.
LinkedHashMap → Maintains insertion order.
Q19. peek(), poll(), remove() in Queue?
peek() → Returns head, doesn’t remove.
poll() → Returns head, removes, returns null if empty.
remove() → Same as poll() but throws exception if empty.
Q20. Enumeration vs Iterator vs ListIterator?
Enumeration → Legacy, read-only.
Iterator → Forward traversal, remove supported.
ListIterator → Bi-directional traversal, add/remove supported.
Q21. What is load factor in HashMap?
Load factor = 0.75 by default → rehash occurs when 75% full.
Q22. Two keys same hashCode in HashMap?
Stored in same bucket, resolved using equals().
Q23. ConcurrentHashMap vs Collections.synchronizedMap()?
synchronizedMap() → Locks entire map.
ConcurrentHashMap → Segment-level locking.
Q24. CopyOnWriteArrayList?
Creates a new copy on modification → good for read-heavy use.
Q25. WeakHashMap?
Uses weak references for keys → entries removed when key not referenced.
Q26. IdentityHashMap vs HashMap?
IdentityHashMap → Compares keys with `==`.
HashMap → Uses equals() and hashCode().
Q27. TreeMap ordering?
Maintains natural or custom Comparator ordering.
Q28. Custom object as HashMap key?
Must override equals() and hashCode().
Q29. Why override hashCode() with equals()?
Equal objects must have same hashCode, else lookup fails.
Q30. Can HashMap key be mutable?
Yes, but unsafe because hashCode may change → entry becomes unreachable.
Q31. Collection vs Collections vs Arrays?
Collection → Interface.
Collections → Utility class.
Arrays → Utility class for arrays.
Q32. Why String is a good HashMap key?
Immutable, cached hashCode, reliable equals().
Q33. ConcurrentModificationException vs IllegalStateException?
CME → Structural modification during iteration.
ISE → Wrong method call sequence (like double remove).
Q34. Why keys should be immutable in HashMap?
Immutable ensures consistent hashCode and equals().
Q35. NavigableSet and NavigableMap?
NavigableSet → SortedSet + navigation methods (lower, higher).
NavigableMap → SortedMap + navigation (floorKey, ceilingKey).
Q36. Ordered vs Sorted collections?
Ordered → Preserves insertion order.
Sorted → Arranged using Comparator or natural order.
Q37. BlockingQueue vs ConcurrentLinkedQueue?
BlockingQueue → Waits for capacity (producer-consumer).
ConcurrentLinkedQueue → Non-blocking, lock-free.
Q38. Custom object as HashMap key?
Yes, but must override equals() & hashCode().
Q39. PriorityQueue vs TreeSet?
PriorityQueue → Allows duplicates, heap-based.
TreeSet → No duplicates, tree-based, sorted.
Q40. Example: Using HashMap
import java.util.*; class MapExample { public static void main(String[] args) { Map map = new
HashMap<>(); map.put(1,"A"); map.put(2,"B"); System.out.println(map.get(1)); // Output: A } }