Java Collections Framework Interview Questions: ArrayList, HashSet, TreeMap and the Traps
By Preeti Verma
If there is one topic that appears in almost every Java interview, from fresher to senior, it is the Collections Framework. The questions look simple, but the follow-ups reveal how much you really understand.
This guide covers the questions that come up most, with the points a strong answer makes. For the most famous collections question, read HashMap vs ConcurrentHashMap explained simply alongside this.
1. What is the Java Collections Framework?
A set of interfaces and classes for storing and working with groups of objects. The main interfaces are Collection (with List, Set and Queue underneath) and Map, which is not a Collection. A common slip is saying that Map extends Collection. It does not.
2. ArrayList vs LinkedList
- ArrayList is backed by a resizable array. Access by index is O(1). Adding at the end is amortised O(1). Inserting or removing in the middle is O(n), because elements shift.
- LinkedList is a doubly linked list. Access by index is O(n). Adding or removing at a known position is O(1), but finding that position costs O(n).
The honest, senior answer: ArrayList wins in almost every real case, because arrays are cache friendly and the memory overhead is lower. A LinkedList only helps in rare situations, and ArrayDeque is usually a better queue or stack.
3. How does ArrayList grow?
When the internal array is full, it creates a bigger one (about 1.5 times the size) and copies the elements. If you know the size in advance, give it an initial capacity to avoid repeated copying.
4. HashSet, LinkedHashSet and TreeSet
- HashSet: no ordering guarantee, average O(1) add, remove and contains. It is backed by a
HashMap(the elements are the keys). - LinkedHashSet: keeps insertion order, slightly more memory.
- TreeSet: keeps elements sorted, using a red-black tree, so operations are O(log n). It needs elements to be
Comparableor aComparator.
A good scenario answer: "I need unique items in the order they were added, so LinkedHashSet."
5. How does a HashSet detect duplicates?
It uses hashCode() to find the bucket and equals() to compare within it. If you override equals() without hashCode(), two "equal" objects can land in different buckets and both be stored. This is the same contract that applies to HashMap keys. Mention that mutable keys are dangerous: if a key's fields change after insertion, its hash changes and you can no longer find it.
6. HashMap vs Hashtable vs TreeMap vs LinkedHashMap
- HashMap: not thread safe, allows one null key and null values, no ordering.
- Hashtable: legacy, synchronised, no null keys or values. Avoid it in new code.
- ConcurrentHashMap: thread safe with much better concurrency than
Hashtable, no null keys or values. - LinkedHashMap: keeps insertion order (or access order, which is useful for building an LRU cache).
- TreeMap: sorted by key, O(log n).
7. Fail-fast vs fail-safe iterators
A fail-fast iterator (for ArrayList, HashMap) throws ConcurrentModificationException if the collection is structurally changed while you iterate, except through the iterator's own remove(). A fail-safe or weakly consistent iterator (such as those of CopyOnWriteArrayList and ConcurrentHashMap) does not throw, but it may not show the latest changes.
The classic trap: removing from a list inside a for-each loop throws ConcurrentModificationException. The fix is Iterator.remove(), or removeIf.
8. Comparable vs Comparator
- Comparable defines the natural order inside the class itself, with
compareTo. There can be only one. - Comparator defines an order outside the class, with
compare. You can have many, such as sort by name, or by salary.
Show modern style: list.sort(Comparator.comparing(Employee::getSalary).reversed().thenComparing(Employee::getName)). Add that compareTo should be consistent with equals, or sorted sets can behave strangely.
9. Iterator vs ListIterator
Iterator goes forward only and works on any collection. ListIterator works on lists, goes both directions, and can add or replace elements as it goes.
10. PriorityQueue
It is a binary heap. The head is always the smallest element (or the one defined by your comparator), and offer/poll are O(log n). The trap: iterating over a PriorityQueue does not give sorted order. Only poll() does. It is the tool for "top K" problems, and for scheduling by priority.
11. ArrayDeque vs Stack vs LinkedList as a stack or queue
Stack is a legacy class built on Vector and synchronised. Use ArrayDeque for both stacks and queues. It is faster, and it is what the documentation recommends.
12. Immutable and unmodifiable collections
List.of(...),Set.of(...)andMap.of(...)create immutable collections. They rejectnulland throw on modification.Collections.unmodifiableList(list)is only a read-only view of the original. If the original changes, the view changes.List.copyOf(list)makes an immutable copy.Arrays.asList(array)returns a fixed-size list: you can set elements, but you cannot add or remove, and it is backed by the array.
These distinctions are a favourite way to test depth. See also Java 8 interview questions for stream methods that return lists.
13. Which collection would you use for...?
Practise answering this out loud:
- Unique items, no order needed:
HashSet. - Unique, sorted:
TreeSet. - Key to value lookup:
HashMap. - Recently used cache:
LinkedHashMapwith access order, or a proper cache library. - Queue of tasks by priority:
PriorityQueue. - Stack or queue:
ArrayDeque. - Shared between threads:
ConcurrentHashMaporCopyOnWriteArrayList, depending on how often it changes. Read the multithreading questions for more.
14. Time complexity cheat sheet
| Structure | Add | Remove | Search | |---|---|---|---| | ArrayList (end / middle) | O(1) amortised / O(n) | O(n) | O(n) | | LinkedList | O(1) at ends | O(n) to find | O(n) | | HashSet / HashMap | O(1) average | O(1) average | O(1) average | | TreeSet / TreeMap | O(log n) | O(log n) | O(log n) | | PriorityQueue | O(log n) | O(log n) | O(n) |
Add that hash operations degrade if many keys collide, and that since Java 8, a bucket with many collisions turns into a tree, so the worst case improves to O(log n).
Quick coding questions to practise
- Remove duplicates from a list while keeping order.
- Find the first non-repeating character in a string.
- Group a list of employees by department.
- Sort a list of objects by two fields.
- Find the top 3 most frequent words in a text.
Practise this on GenQuest
- Open the question banks: Core Java scenarios, Blind 75 and NeetCode 150, high-level and low-level design questions, and company-wise sheets, with a free preview and progress tracking.
- Start a voice mock interview: answer out loud to an AI interviewer that knows your resume and the role, get follow-up questions, then read a scored report with a question-by-question review. Your first 15-minute interview is free.
- Ask the AI Coach: paste any question you are stuck on and get it explained in plain words, with a short answer you could say in an interview. See how it works in our AI Coach introduction.
FAQ
Which collection questions are asked most for freshers? ArrayList vs LinkedList, HashMap internals, HashSet duplicates, and Comparable vs Comparator.
Do I need to know internal implementation details? For HashMap and ArrayList, yes, at a high level. For the others, know the structure (tree, heap, linked list) and the complexity.
Is Vector still used? Rarely. Know it is synchronised and legacy.
The takeaway
Know the main structures, their complexity and the traps: equals/hashCode, fail-fast iterators, Arrays.asList, and unmodifiable views. Answer with a short reason and a real use case, and you will handle most Collections rounds.