Java Collections Interview Questions
Last updated 2026-11-19
The collections framework is the most heavily tested library area in Java interviews, because every real application uses it and every wrong choice has a measurable cost. Questions fall into three types: choosing the right structure, predicting what a snippet prints, and explaining why a modification throws an exception.
HashMap deserves a deeper treatment than fits here, so it has its own page on HashMap internals. For coding-round practice, see DSA interview questions.
| Core interfaces | List, Set, Queue, Deque, Map |
|---|---|
| Java version | 17 and later, with notes on 21 |
| Typical rounds | Written test and first technical round |
Choosing a collection quickly
- Need index access and fast iteration: ArrayList.
- Need a stack or queue: ArrayDeque, not Stack or LinkedList.
- Need uniqueness without order: HashSet; insertion order: LinkedHashSet; sorted: TreeSet.
- Need key lookup: HashMap; sorted keys: TreeMap; thread-safe: ConcurrentHashMap.
- Need smallest or largest first: PriorityQueue.
Java Collections Interview Questions: questions with worked answers
1 What is the difference between Collection and Collections, and is Map a Collection?
easyCollection is the root interface of List, Set and Queue. Collections is a utility class with static helpers such as sort, reverse, shuffle, unmodifiableList and synchronizedList. Map is not a Collection: it stores key-value pairs and sits in a separate hierarchy, although its keySet(), values() and entrySet() views are collections.
2 ArrayList versus LinkedList: which is faster and when?
easyArrayList is a resizable array: get(i) is O(1), adding at the end is amortised O(1) and inserting or removing in the middle is O(n) because elements shift. LinkedList is a doubly linked list: get(i) is O(n), and insertion or removal is O(1) only once you already hold the node, for example through an iterator. In practice ArrayList wins for almost every workload because contiguous memory is cache friendly and LinkedList carries a node object of 24 or more bytes per element.
3 How does ArrayList grow?
mediumThe no-argument constructor creates an empty shared array and the first add allocates capacity 10. When full, it grows to oldCapacity + (oldCapacity >> 1), about 1.5 times, by copying into a new array. If you know the size, pass it to the constructor or call ensureCapacity to avoid repeated copies.
4 Predict the output: removing inside a for-each loop.
mediumRemoving during a for-each normally throws ConcurrentModificationException, because the iterator compares a modCount snapshot on each next(). The exception to remember is removing the second-to-last element: afterwards size is 2 and the cursor is 2, so hasNext() returns false and the loop ends without any exception.
javaList<Integer> a = new ArrayList<>(List.of(1, 2, 3)); for (Integer x : a) if (x == 2) a.remove(x); System.out.println(a); // [1, 3], no exception List<Integer> b = new ArrayList<>(List.of(1, 2, 3)); for (Integer x : b) if (x == 1) b.remove(x); // ConcurrentModificationException on the next iteration5 What is fail-fast versus fail-safe, and how do you remove safely while iterating?
mediumFail-fast iterators of ArrayList, HashMap and most java.util collections throw ConcurrentModificationException when the structure changes outside the iterator, on a best-effort basis. Collections in java.util.concurrent, such as CopyOnWriteArrayList and ConcurrentHashMap, iterate over a snapshot or a weakly consistent view and never throw it. To remove safely use Iterator.remove(), or the simpler removeIf(predicate) available since Java 8.
javaList<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4)); list.removeIf(x -> x % 2 == 0); System.out.println(list); // [1, 3]6 Arrays.asList versus List.of versus new ArrayList: what can each do?
mediumArrays.asList returns a fixed-size list backed by the array: set works and writes through to the array, add or remove throws UnsupportedOperationException, and nulls are allowed. List.of returns an immutable list: any change throws UnsupportedOperationException and null elements throw NullPointerException. new ArrayList copies into a fully mutable list. Also, Arrays.asList on an int[] gives a List<int[]> with one element because primitives are not generics.
javaInteger[] arr = {1, 2, 3}; List<Integer> view = Arrays.asList(arr); view.set(0, 9); System.out.println(arr[0]); // 9 // view.add(4); // UnsupportedOperationException int[] prim = {1, 2, 3}; System.out.println(Arrays.asList(prim).size()); // 17 What does list.remove(1) do on a List<Integer>?
mediumThere are two overloads: remove(int index) and remove(Object o). A literal 1 is an int, so it picks the index version and removes the element at position 1. To remove the value 1 write list.remove(Integer.valueOf(1)). This is a classic output trap.
javaList<Integer> l = new ArrayList<>(List.of(10, 1, 20)); l.remove(1); // removes index 1 (the value 1) System.out.println(l); // [10, 20] List<Integer> m = new ArrayList<>(List.of(10, 1, 20)); m.remove(Integer.valueOf(20)); // removes the value 20 System.out.println(m); // [10, 1]8 HashSet, LinkedHashSet and TreeSet: how do they differ?
easyHashSet has no ordering guarantee and O(1) average add and contains. LinkedHashSet keeps insertion order using a linked list through the entries at slightly higher cost. TreeSet keeps elements sorted using a red-black tree, with O(log n) operations, and requires elements to be Comparable or a Comparator to be supplied. TreeSet uses compareTo for equality, so two objects that compare as 0 are treated as duplicates even if equals says otherwise.
9 What is the difference between Comparable and Comparator? Show a multi-field sort.
mediumComparable defines the natural order inside the class through compareTo, one order per class. Comparator is an external strategy, so you can have many orders. Build them with Comparator.comparing and thenComparing instead of manual subtraction, which can overflow.
javarecord Emp(String name, int age) {} List<Emp> list = new ArrayList<>(List.of( new Emp("Ravi", 30), new Emp("Asha", 25), new Emp("Dev", 30))); list.sort(Comparator.comparingInt(Emp::age).reversed() .thenComparing(Emp::name)); System.out.println(list); // [Emp[name=Dev, age=30], Emp[name=Ravi, age=30], Emp[name=Asha, age=25]]10 Predict the output of printing a PriorityQueue. Is iteration sorted?
hardNo. A PriorityQueue is a binary min-heap in an array; only the head is guaranteed to be the smallest. toString and the iterator walk the array in heap order, so the printout looks partly sorted. Polling repeatedly does return sorted order.
javaPriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(5); pq.add(1); pq.add(3); System.out.println(pq); // [1, 5, 3] System.out.println(pq.poll()); // 1 System.out.println(pq.poll()); // 311 How do null keys and values behave across Map implementations?
mediumHashMap and LinkedHashMap allow one null key and many null values. TreeMap throws NullPointerException for a null key with natural ordering. Hashtable and ConcurrentHashMap reject null keys and values, partly because a null return from get cannot be distinguished from a missing key in a concurrent setting. Map.of and Map.copyOf also reject nulls.
12 Why is ArrayDeque preferred over Stack and LinkedList for stacks and queues?
easyStack extends Vector, so every method is synchronised and it exposes index operations that break the stack abstraction. ArrayDeque is an unsynchronised circular array with O(1) push, pop, offer and poll, and it does not allow nulls. It is usually faster than LinkedList because it allocates no node per element. Use Deque<Integer> stack = new ArrayDeque<>() with push and pop.
13 What is the difference between Collections.unmodifiableList and List.copyOf?
mediumunmodifiableList is a read-only view: changes to the original list show through the view. List.copyOf takes an immutable snapshot, so later changes to the source are invisible. If the source is already an immutable list created by List.of, copyOf returns the same instance without copying.
14 Why can a mutable object break a HashSet or HashMap key?
hardThe bucket is chosen from hashCode at insertion time. If a field used in hashCode changes afterwards, contains and remove look in a different bucket and fail, and the entry becomes unreachable though still stored. Use immutable keys such as String, Integer or records.
javaSet<List<Integer>> set = new HashSet<>(); List<Integer> key = new ArrayList<>(List.of(1)); set.add(key); key.add(2); // hashCode changes System.out.println(set.contains(key)); // false System.out.println(set.size()); // 115 What changed in Java 21 for ordered collections?
mediumJava 21 added the SequencedCollection, SequencedSet and SequencedMap interfaces. List, Deque, LinkedHashSet, LinkedHashMap, TreeSet and TreeMap now share methods such as getFirst, getLast, addFirst, removeLast and reversed(), which returns a view. Before 21 you needed list.get(list.size() - 1) or different methods per type.
16 Is Collections.sort stable and what algorithm does it use?
mediumYes. List.sort and Collections.sort use TimSort for objects, a merge-sort and insertion-sort hybrid that is stable, so equal elements keep their relative order, with O(n log n) worst case and O(n) on nearly sorted input. Arrays.sort on primitives uses dual-pivot quicksort, which is not stable, but stability is meaningless for primitives.
Frequently asked questions
Which collection questions are asked most?
ArrayList versus LinkedList, HashMap internals, fail-fast iterators, Comparable versus Comparator and HashSet versus TreeSet appear in most Java technical rounds.
Is HashMap thread safe?
No. Concurrent writes can lose updates or corrupt the table. Use ConcurrentHashMap, or guard access with locks.
Do I need to know the time complexity of every collection?
You should know the big O of add, get, remove and contains for ArrayList, LinkedList, HashMap, TreeMap, HashSet and PriorityQueue.
How do I practise collection problems?
Solve array and hashing problems from the DSA hub in Java and use the standard collection classes instead of arrays.
Related guides
More in Programming Language Interview Questions by Language
- Java Concurrency Utilities Interview Questions
- Java Exception Handling Interview Questions
- Java Garbage Collection Interview Questions
- Java Generics Interview Questions
- Java HashMap Internals Interview Questions
- Java Interfaces and Abstract Classes Interview Questions
- Java Interview Questions and Answers
- Java Multithreading Interview Questions
- Java OOPs Interview Questions
- Java String Interview Questions
- JavaScript Array Methods Interview Questions
- JavaScript Closures Interview Questions