Java Collections – Top 35 Interview
Questions & Answers
Q1. What is Java Collection Framework?
The Java Collection Framework is a set of classes and interfaces that implement commonly
reusable collection data structures like List, Set, Map, etc. It provides algorithms to
manipulate them, such as sorting and searching.
Q2. What is the difference between List and Set?
List allows duplicates and maintains insertion order. Set does not allow duplicates and may
or may not maintain order (e.g., HashSet vs LinkedHashSet).
Q3. What are the major interfaces in the Collection Framework?
The key interfaces are Collection, List, Set, Queue, and Map. These define basic operations
and behaviors for groups of objects.
Q4. What is the difference between ArrayList and LinkedList?
ArrayList provides fast random access and is better for search-heavy tasks. LinkedList is
better when you do frequent insertions/deletions.
Q5. Difference between HashSet and TreeSet?
HashSet is unordered and allows null. TreeSet stores elements in sorted order and does not
allow null.
Q6. Why is Map not a part of Collection interface?
Because Map works with key-value pairs, not single elements like Collection does. It has a
different structure and usage.
Q7. Difference between HashMap and Hashtable?
HashMap is not synchronized and allows one null key. Hashtable is synchronized and
doesn’t allow null keys or values.
Q8. How does HashMap work internally?
It uses an array of buckets and stores entries using the hashcode of the key. Collisions are
resolved using LinkedList or Tree (since Java 8).
Q9. What is LinkedHashMap?
It is like HashMap but maintains insertion order using a doubly linked list.
Q10. What is TreeMap?
TreeMap stores entries sorted by keys using a Red-Black Tree. It does not allow null keys.
Q11. Difference between HashMap and TreeMap?
HashMap is unordered, TreeMap is sorted. HashMap is faster, TreeMap useful when
ordering of keys is needed.
Q12. What is ConcurrentHashMap?
Thread-safe version of HashMap. It allows concurrent reads and segmented locking for
writes.
Q13. Difference between fail-fast and fail-safe iterators?
Fail-fast throws ConcurrentModificationException on structural modification. Fail-safe
iterates over a copy.
Q14. What is the use of EnumSet?
EnumSet is a high-performance Set for enum types. It’s compact and fast because it uses bit
vectors.
Q15. Difference between Collection and Collections?
`Collection` is an interface. `Collections` is a utility class with static methods like sort(),
reverse(), etc.
Q16. What is the use of Vector class?
Vector is a legacy synchronized version of ArrayList. Not preferred for modern apps.
Q17. What is Stack in Java?
Stack is a subclass of Vector that follows LIFO (Last In First Out). It uses push(), pop(),
peek() methods.
Q18. How to sort a list in Java?
Use Collections.sort(list) for natural order, or provide a Comparator for custom sorting.
Q19. Difference between Comparable and Comparator?
Comparable is used to define default sort order inside class using compareTo(). Comparator
is external sorting logic.
Q20. What is CopyOnWriteArrayList?
A thread-safe version of ArrayList used when reads are frequent and writes are rare.
Q21. What is EnumSet and how is it different from other sets?
EnumSet is for enum types, internally uses bit vectors, very fast, doesn’t allow nulls, and is
type-safe.
Q22. Difference between ArrayDeque and LinkedList as Deque?
ArrayDeque is faster and more memory-efficient. LinkedList allows nulls, but is slower and
uses more memory.
Q23. How is CopyOnWriteArrayList different from ArrayList?
CopyOnWriteArrayList creates a new copy on write operations. It’s thread-safe and avoids
ConcurrentModificationException.
Q24. What is the difference between Collections and Collection?
Collection is an interface. Collections is a utility class containing static methods for
collection operations.
Q25. Real-world use cases for Map implementations?
HashMap → basic key-value store, TreeMap → sorted data, LinkedHashMap → LRU Cache,
ConcurrentHashMap → thread-safe store.
Q26. How does Collections.sort() work internally?
It uses TimSort, which is a hybrid of merge sort and insertion sort. Efficient for real-world
data.
Q27. Difference between Comparable and Comparator?
Comparable defines default order inside the class. Comparator provides custom logic
outside the class.
Q28. What is Collections.unmodifiableList()?
Returns a read-only view of a list. Any modification attempt throws
UnsupportedOperationException.
Q29. How to remove duplicates from a List?
Use new HashSet<>(list), or use streams: list.stream().distinct().collect(Collectors.toList()).
Q30. What is NavigableMap?
A sub-interface of SortedMap. Allows navigation methods like lowerKey(), floorKey(),
ceilingKey().
Q31. How is Hashtable different from HashMap?
Hashtable is synchronized, no null keys or values. HashMap is faster, allows one null key
and many null values.
Q32. Difference between fail-fast and fail-safe iterators?
Fail-fast throws ConcurrentModificationException. Fail-safe works on a copy (e.g.,
CopyOnWriteArrayList).
Q33. How to implement LRU Cache?
Use LinkedHashMap with accessOrder=true and override removeEldestEntry().
Q34. Why are sets unordered? How to preserve order?
HashSet doesn’t maintain order. Use LinkedHashSet for insertion order. Use TreeSet for
sorted order.
Q35. Which is faster: ArrayList or LinkedList? Why?
ArrayList is faster for random access (O(1)). LinkedList is faster for frequent add/remove at
ends.