Java Collections is one of the most frequently asked topics in technical interviews—from junior developer roles to senior system design rounds. Understanding how different collections behave under the hood, when to use what, and how to optimize for memory and performance can give you a competitive edge in interviews.
In this comprehensive guide, we’ll cover Java Collections Interview Questions ranging from basic to advanced, with explanations, best practices, and real-world analogies.
Core Concepts
Java Collections Framework provides key data structures for storing, manipulating, and retrieving data. Core interfaces include:
- List: Ordered collection (e.g., ArrayList, LinkedList)
- Set: Unique elements (e.g., HashSet, TreeSet)
Map: Key-value pairs (e.g.,HashMap,TreeMap)QueueandDeque: FIFO/LIFO ordering
Top Basic Interview Questions
1. What is the difference between List, Set, and Map?
| Feature | List | Set | Map |
|---|---|---|---|
| Duplicates | Allowed | Not Allowed | Only keys disallow duplicates |
| Order | Maintained (ArrayList) |
Not guaranteed (HashSet) |
Keys are unordered unless LinkedHashMap or TreeMap |
2. What is the default capacity of an ArrayList?
- 10 in Java 8+
3. How does HashSet work internally?
- Backed by a
HashMapwith dummy values.
4. What is the load factor in HashMap?
- Default:
0.75. Controls when the map resizes.
Intermediate and Real-World Scenarios
1. How would you prevent ConcurrentModificationException?
- Use an
Iteratoranditerator.remove()OR use concurrent collections likeCopyOnWriteArrayList.
2. How would you implement LRU Cache?
- Use
LinkedHashMapwith access-order and overrideremoveEldestEntry.
3. How do you sort a list of custom objects?
Collections.sort(list, Comparator.comparing(Employee::getName));
Advanced Internals and Performance Questions
1. How does HashMap handle collisions?
- Uses chaining with linked lists, and from Java 8 onwards, switches to balanced trees (red-black trees) if bucket size exceeds threshold.
2. What happens if two keys have the same hashcode?
- The
equals()method is used to resolve collision.
3. Why is ConcurrentHashMap preferred over synchronized HashMap?
- It uses bucket-level locking or segment-based locking to improve concurrency.
4. How does ArrayList grow?
- Increases capacity by 50% on every resize.
Comparisons: HashMap vs TreeMap, List vs Set
| Feature | HashMap | TreeMap |
|---|---|---|
| Ordering | Unordered | Sorted by keys |
| Null keys | Allowed (1) | Not allowed |
| Performance | O(1) for get/put | O(log n) |
| Feature | List | Set |
|---|---|---|
| Duplicates | Allowed | Not allowed |
| Access | Index-based | Value-based |
Version-specific Enhancements
Java 8
- Streams API
Collectors,Optional,Map.computeIfAbsent()
Java 9
- Factory methods like
List.of(),Set.of(),Map.of()
Java 10+
vartype inference
Java 21
- Runtime performance improvements for collections
- Structured concurrency (related to parallel stream use)
Functional Programming in Interviews
Sample question:
How do you group a list of employees by department using Streams?
Map<String, List<Employee>> grouped = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
Best Practices for Interview Answers
- Use real-world analogies: e.g., “A
HashMapis like a set of drawers labeled by key.” - Know trade-offs:
ArrayListvsLinkedList,HashMapvsTreeMap - Emphasize immutability in multi-threaded contexts
📌 What's New in Java Versions?
- Java 8
- Stream API,
Collectors,Optional, Lambdas
- Stream API,
- Java 9
List.of(),Map.of(), immutable factory methods
- Java 10
vartype inference
- Java 21
- Memory optimizations for HashMap, structured concurrency
Conclusion and Key Takeaways
Mastering Java Collections is essential for cracking interviews. Whether you're asked about data structure selection, memory models, or real-world scenarios—knowing your collections deeply can help you stand out.
FAQ
1. Is HashMap ordered?
No. Use LinkedHashMap or TreeMap if ordering is important.
2. Can a Set contain null?
Yes. HashSet allows one null. TreeSet throws exception.
3. What’s the difference between ArrayList and LinkedList?
ArrayList is faster for access, LinkedList for insert/remove.
4. Can you use null keys in HashMap?
Yes, only one null key is allowed.
5. How do you prevent resizing in HashMap?
Pre-size with initialCapacity = expectedSize / loadFactor + 1.
6. When to use HashSet vs TreeSet?
HashSet for performance; TreeSet when sorting is required.
7. Does ConcurrentHashMap allow nulls?
No. Neither null keys nor null values.
8. What is fail-fast behavior?
Modifying a collection while iterating throws ConcurrentModificationException.
9. Can you sort a Map by values?
Yes, by converting entrySet() to a list and using a comparator.
10. What is the time complexity of HashMap.get()?
Average case O(1), worst case O(log n) with tree bins (Java 8+).