100 Java Collections interview questions



 Here are 100 Java Collections interview questions, organized by level:



Beginner (30 Questions)

  1. What is the Java Collections Framework and why is it useful?

  2. What’s the difference between Collection and Collections?

  3. Name the core interfaces of the Collections Framework.

  4. How do List, Set, Queue, and Map differ conceptually?

  5. Explain when you’d use an ArrayList vs. a LinkedList.

  6. What are the key differences between HashSet, TreeSet, and LinkedHashSet?

  7. How does a HashMap differ from a Hashtable?

  8. What is fail-fast behavior in Collections?

  9. What exception is thrown on concurrent modification of a Collection?

  10. How do you iterate over a List? Name at least three ways.

  11. What’s the difference between Iterator and ListIterator?

  12. How do you make a List thread-safe using Collections?

  13. What is the purpose of the Collections utility class?

  14. How do you sort a List of custom objects?

  15. What is a functional interface in java.util.function and name three examples?

  16. How do you convert an array to a List?

  17. Explain the difference between Comparable and Comparator.

  18. How do you shuffle elements in a List?

  19. What is Iterable and why is it important?

  20. How does Map fit into the Collections Framework?

  21. Name the common Map implementations and their ordering guarantees.

  22. How do you retrieve keys, values, and entries from a Map?

  23. What is a SortedMap and how does it differ from a NavigableMap?

  24. How do you create an immutable List or Map prior to Java 9?

  25. What is the default initial capacity and load factor of a HashMap?

  26. What happens internally when you call put() on a HashMap?

  27. How does TreeMap maintain its sort order?

  28. Explain the concept of hashing and hash codes in Java.

  29. What are the drawbacks of using Vector today?

  30. How do you clear all elements from a Collection in one call?


Intermediate (40 Questions)

  1. How does ConcurrentModificationException occur, and how can you avoid it?

  2. Describe weakly-consistent vs. fail-fast iterators.

  3. What is CopyOnWriteArrayList and when would you use it?

  4. How does ConcurrentHashMap work under the hood (pre-Java 8 segment model)?

  5. What changed in ConcurrentHashMap in Java 8 regarding tree bins?

  6. Explain BlockingQueue and name two implementations.

  7. When would you choose ArrayBlockingQueue over LinkedBlockingQueue?

  8. How does PriorityQueue determine element order?

  9. What is the Deque interface, and how do ArrayDeque and LinkedList implement it?

  10. How do EnumSet and EnumMap work internally?

  11. Explain IdentityHashMap and a use case for it.

  12. What is a WeakHashMap, and how does it interact with the garbage collector?

  13. How do you implement an LRU cache using LinkedHashMap?

  14. What are bulk operations in Collection (e.g., addAll, removeAll)?

  15. How does List.subList() work and what pitfalls does it have?

  16. What’s the difference between a shallow copy and a deep copy of a Collection?

  17. How do you sort a Map by its values?

  18. Explain Collections.unmodifiableList(), emptyList(), and singletonList().

  19. How do you perform a binary search on a List? What prerequisites must hold?

  20. What is the complexity of key operations (get, put, remove) in HashMap and TreeMap?

  21. How does Collections.reverseOrder() work with Comparators?

  22. What does Collections.synchronizedMap() do internally?

  23. How do you iterate over a ConcurrentHashMap without blocking writes?

  24. Explain Stream vs. Collection—when to use each.

  25. How do you convert a Stream back into a List or Map?

  26. What are computeIfAbsent and merge methods in Map, and why use them?

  27. How do you handle collisions in a HashMap?

  28. Explain the role of Spliterator in the parallel processing of Collections.

  29. What are the main characteristics (ORDERED, SIZED, etc.) of a Spliterator?

  30. How do you use Collections.checkedList() and why?

  31. What is ConcurrentLinkedQueue and how does it differ from LinkedBlockingQueue?

  32. How would you implement a thread-safe stack without using Vector?

  33. Explain the concept of fail-safe vs. fail-fast on concurrent collections.

  34. What is a NavigableSet and how does it extend SortedSet?

  35. Describe how TreeSet implements NavigableSet.

  36. How do you remove elements safely while iterating a Collection?

  37. How does Arrays.asList() differ from new ArrayList<>(Arrays.asList())?

  38. What is the time complexity of contains() in ArrayList vs. LinkedList?

  39. How do you implement a priority queue with a custom comparator?

  40. Explain how Collections.frequency() works.


Advanced (30 Questions)

  1. Detail the internal structure of HashMap in Java 8 (array, tree bins, linked lists).

  2. What triggers treeification of a HashMap bucket?

  3. How does resizing work in HashMap, and what is its complexity?

  4. What are tombstones in HashMap (removed entries)?

  5. How do LinkedHashMap’s access-order vs. insertion-order modes differ?

  6. Explain how HashSet uses HashMap internally.

  7. How is serialization of Collections handled in Java?

  8. What is the contract between equals() and hashCode() for use in Collections?

  9. Describe the red-black tree properties in TreeMap.

  10. How can you implement a custom sorted collection?

  11. How would you write your own Collection class from scratch?

  12. Explain the roles of AbstractCollection, AbstractList, and AbstractMap.

  13. How can you implement a high-performance ring buffer in Java?

  14. Discuss JVM memory considerations when working with large Collections.

  15. How do you minimize garbage generation when processing Collections?

  16. Explain how ParallelStream decides to split work across threads.

  17. How would you profile and optimize a slow Collection-heavy code path?

  18. How do you implement a bounded concurrent cache with eviction policies?

  19. Describe how backpressure can be applied when consuming fast producers into Collections.

  20. What techniques exist to avoid false sharing in concurrent Collections?

  21. Explain the differences between open addressing and separate chaining in hash tables.

  22. How do weak references and reference queues enable cache implementations?

  23. Discuss custom Comparator implementations that are both efficient and null-safe.

  24. How do you implement a lock-free queue in Java?

  25. What considerations apply when serializing concurrent Collections?

  26. How would you design a multi-level caching structure using Collections?

  27. Discuss strategies for safely publishing Collections in a concurrent setting.

  28. How can you use sun.misc.Unsafe (or VarHandle) to build custom Collections?

  29. What future enhancements would you propose for the Java Collections Framework?

  30. Compare the Java Collections Framework to analogous frameworks in other languages (e.g., C#, C++ STL).


Use these questions to guide your study, mock interviews, and whiteboarding sessions. Good luck!



Comments