Java Collections Interview Questions: 15 Must-Know Answers
Java Collections interview answers: HashMap internals, ArrayList vs LinkedList, HashSet, TreeMap, fail-fast iterators and ConcurrentHashMap.
The Java Collections Framework is one of the most reliably tested topics in Java interviews at every level, from fresher roles at IT services companies to senior backend roles at product companies. Interviewers use it to check whether you understand data structures in practice: which collection to choose, how it works internally, and what goes wrong under concurrency. Here are fifteen questions that come up often, with concise answers.
1. What is the Collections Framework hierarchy?
The main interfaces are `Collection` (with sub-interfaces `List`, `Set` and `Queue`/`Deque`) and the separate `Map` interface. Common implementations: `ArrayList`, `LinkedList` (List and Deque); `HashSet`, `LinkedHashSet`, `TreeSet` (Set); `PriorityQueue`, `ArrayDeque` (Queue/Deque); `HashMap`, `LinkedHashMap`, `TreeMap`, `ConcurrentHashMap` (Map).
2. ArrayList vs LinkedList
`ArrayList` is backed by a resizable array: O(1) random access by index, amortised O(1) append, O(n) inserts and removals in the middle because elements shift. `LinkedList` is a doubly linked list: O(n) access by index, O(1) insert or remove once you have the node position. In practice `ArrayList` is the better default for most workloads because of cache-friendly memory layout; use `ArrayDeque` rather than `LinkedList` for queue and stack operations.
3. How does HashMap work internally?
`HashMap` stores entries in an array of buckets. For a key, it computes `hashCode()`, spreads the bits, and maps it to a bucket index. Entries with the same bucket form a linked list; since Java 8, a bucket with many entries is converted to a balanced tree once the table is large enough, improving worst-case lookups from O(n) to O(log n). When the number of entries exceeds capacity × load factor (default 0.75), the table doubles and entries are redistributed.
4. Why must equals() and hashCode() be consistent?
If two objects are equal by `equals()`, they must return the same `hashCode()`. Otherwise a `HashMap` or `HashSet` may place them in different buckets and fail to find an existing key, causing duplicates or missing lookups.
Records in modern Java generate both correctly.
5. What happens if you use a mutable object as a HashMap key?
If a field used in `hashCode()` changes after insertion, the entry stays in the old bucket and lookups with the modified key fail. Use immutable keys such as `String`, wrapper types, records or immutable classes.
6. HashMap vs Hashtable vs ConcurrentHashMap
`Hashtable` is a legacy synchronised map that locks the whole table on every operation and does not allow null keys or values. `HashMap` is not thread-safe and allows one null key. `ConcurrentHashMap` is designed for concurrent access with much finer-grained locking and lock-free reads in most cases; it does not allow null keys or values. For concurrent code, prefer `ConcurrentHashMap`.
7. HashSet internals
`HashSet` is backed by a `HashMap`: elements are stored as keys with a shared dummy value. That is why element uniqueness depends on `equals()` and `hashCode()`.
8. HashMap vs LinkedHashMap vs TreeMap
`HashMap` has no ordering guarantees. `LinkedHashMap` maintains insertion order (or access order, useful for LRU caches). `TreeMap` keeps keys sorted, using natural ordering or a `Comparator`, with O(log n) operations and navigation methods like `floorKey` and `headMap`.
9. Build an LRU cache with LinkedHashMap
A frequent interview question; also be ready to implement it with a `HashMap` and a doubly linked list.
10. Comparable vs Comparator
`Comparable` defines a class's natural ordering through `compareTo`. `Comparator` defines an external ordering, so you can sort the same class in different ways.
11. What are fail-fast and fail-safe iterators?
Iterators of most standard collections are fail-fast: if the collection is structurally modified during iteration other than through the iterator, they throw `ConcurrentModificationException` on a best-effort basis. Concurrent collections like `ConcurrentHashMap` and `CopyOnWriteArrayList` provide weakly consistent iterators that do not throw and may or may not reflect concurrent changes.
12. How do you remove elements safely while iterating?
Calling `names.remove(...)` inside a for-each loop typically causes `ConcurrentModificationException`.
13. What is PriorityQueue and how does it work?
A binary heap-based queue that returns the smallest element (or largest, with a reversed comparator). `offer` and `poll` are O(log n), `peek` O(1). Iterating a `PriorityQueue` does not return elements in sorted order. Common uses: top-K problems, scheduling, Dijkstra's algorithm.
14. Unmodifiable vs immutable collections
`Collections.unmodifiableList(list)` returns a read-only view; changes to the underlying list are still visible. `List.of(...)` and `List.copyOf(...)` create truly unmodifiable collections that also reject nulls. Prefer them for constants and return values that callers should not change.
15. How do you choose a collection?
A quick decision guide:
Interviewers often follow up with "Where have you used this?" Prepare one real example per major collection — a `TreeMap` for time-window lookups, a `ConcurrentHashMap` for a cache, a `PriorityQueue` for scheduling — from projects or work.
- Need index access and mostly append? `ArrayList`.
- Need a queue or stack? `ArrayDeque`.
- Need uniqueness? `HashSet`; with insertion order, `LinkedHashSet`; sorted, `TreeSet`.
- Need key-value lookup? `HashMap`; ordered, `LinkedHashMap`; sorted or range queries, `TreeMap`.
- Need priority ordering? `PriorityQueue`.
- Shared across threads? `ConcurrentHashMap`, `CopyOnWriteArrayList` for read-heavy lists, or a `BlockingQueue` for producer-consumer.
Getting to the interview
Make sure your resume names Java and the frameworks you use in context; the Algnetic ATS resume builder checks it against target postings, and Naukri auto-apply can keep applications going to Java roles while you prepare.
Key takeaways
- Know the hierarchy and the trade-offs between core implementations.
- Understand HashMap internals and the equals/hashCode contract deeply.
- Know ordering differences between HashMap, LinkedHashMap and TreeMap.
- Understand fail-fast iteration and concurrent collections.
- Be able to choose and justify a collection for a given problem.
Let Algnetic apply to matching jobs for you · More career guides
Related guides
https://algnetic.com/blog/java-collections-framework-interview-questions