Java Collections – Top 50 Interview Questions & Answers
1️⃣ What is the Java Collection Framework?
A unified architecture to store, retrieve, and manipulate groups of objects using interfaces like List, Set, and Map.
2️⃣ Why do we need collections?
To handle dynamic data, reduce coding effort, improve performance, and provide reusable data structures.
3️⃣ What are the main interfaces in the Collection Framework?
-
List -
Set -
Queue -
Deque -
Map(part of framework but not a child ofCollection)
4️⃣ Difference between Collection and Collections?
| Collection | Collections |
|---|---|
| Interface | Utility class |
| Represents data | Helper methods |
5️⃣ Difference between List, Set, and Map?
| Type | 특징 |
|---|---|
| List | Allows duplicates, ordered |
| Set | No duplicates |
| Map | Key–value pairs |
6️⃣ What is an ArrayList?
A resizable array implementation of List.
7️⃣ Difference between ArrayList and LinkedList?
| ArrayList | LinkedList |
|---|---|
| Fast read | Fast insert/delete |
| Uses array | Uses doubly linked list |
8️⃣ What is Vector?
A legacy synchronized list (rarely used now).
9️⃣ Difference between ArrayList and Vector?
| ArrayList | Vector |
|---|---|
| Not synchronized | Synchronized |
| Faster | Slower |
🔟 What is a Set?
A collection that does not allow duplicates.
1️⃣1️⃣ Difference between HashSet and TreeSet?
| HashSet | TreeSet |
|---|---|
| Unordered | Sorted |
| Faster | Slower |
1️⃣2️⃣ What is LinkedHashSet?
Maintains insertion order while preventing duplicates.
1️⃣3️⃣ How does HashSet work internally?
Uses a HashMap internally to store elements as keys.
1️⃣4️⃣ What is a Map?
Stores data in key–value pairs.
1️⃣5️⃣ Difference between HashMap and Hashtable?
| HashMap | Hashtable |
|---|---|
| Not synchronized | Synchronized |
| Allows null | No null |
1️⃣6️⃣ Difference between HashMap and LinkedHashMap?
| HashMap | LinkedHashMap |
|---|---|
| No order | Maintains insertion order |
1️⃣7️⃣ Difference between HashMap and TreeMap?
| HashMap | TreeMap |
|---|---|
| Unordered | Sorted |
| Faster | Slower |
1️⃣8️⃣ How does HashMap work internally?
Uses hashing, buckets, and linked lists / red-black trees (Java 8+).
1️⃣9️⃣ What is load factor?
Determines when a HashMap resizes (default = 0.75).
2️⃣0️⃣ What happens when HashMap is full?
It resizes and rehashes entries.
2️⃣1️⃣ What is collision in HashMap?
When multiple keys produce the same hash index.
2️⃣2️⃣ How are collisions handled?
Using:
-
Linked list (Java 7)
-
Linked list → Red-Black Tree (Java 8+)
2️⃣3️⃣ What is ConcurrentHashMap?
A thread-safe, high-performance Map.
2️⃣4️⃣ Difference between HashMap and ConcurrentHashMap?
| HashMap | ConcurrentHashMap |
|---|---|
| Not thread-safe | Thread-safe |
| Allows null | No null |
2️⃣5️⃣ Why ConcurrentHashMap is faster than Hashtable?
Uses bucket-level locking instead of full synchronization.
2️⃣6️⃣ What is Queue?
A collection for holding elements before processing (FIFO).
2️⃣7️⃣ What is PriorityQueue?
Queue where elements are ordered by priority.
2️⃣8️⃣ Difference between Queue and Deque?
| Queue | Deque |
|---|---|
| One-end operations | Both ends |
2️⃣9️⃣ What is ArrayDeque?
A resizable array implementation of Deque.
3️⃣0️⃣ What is an Iterator?
Used to traverse a collection safely.
3️⃣1️⃣ Difference between Iterator and ListIterator?
| Iterator | ListIterator |
|---|---|
| Forward only | Bidirectional |
| All collections | List only |
3️⃣2️⃣ What is fail-fast iterator?
Throws ConcurrentModificationException if collection is modified.
3️⃣3️⃣ What is fail-safe iterator?
Works on a copy of the collection (no exception).
3️⃣4️⃣ Example of fail-safe collection?
CopyOnWriteArrayList
3️⃣5️⃣ What is Collections.sort()?
Utility method to sort a list.
3️⃣6️⃣ Difference between Comparable and Comparator?
| Comparable | Comparator |
|---|---|
| Natural order | Custom order |
compareTo() | compare() |
3️⃣7️⃣ Can we sort a Map?
Directly ❌
Convert to:
-
List<Map.Entry> -
or use
TreeMap
3️⃣8️⃣ What is immutability in collections?
Unmodifiable collections cannot be changed.
3️⃣9️⃣ How to create immutable collections?
4️⃣0️⃣ What is EnumMap?
A Map optimized for enum keys.
4️⃣1️⃣ What is WeakHashMap?
Keys are weakly referenced → garbage collected when unused.
4️⃣2️⃣ What is IdentityHashMap?
Uses == instead of equals() for key comparison.
4️⃣3️⃣ What is SynchronizedMap?
Thread-safe wrapper using Collections.synchronizedMap().
4️⃣4️⃣ Difference between synchronizedMap and ConcurrentHashMap?
| synchronizedMap | ConcurrentHashMap |
|---|---|
| Single lock | Segment/bucket locking |
| Slower | Faster |
4️⃣5️⃣ What is CopyOnWriteArrayList?
Thread-safe list optimized for read-heavy scenarios.
4️⃣6️⃣ What is NavigableMap?
Provides navigation methods like lowerKey(), higherKey().
4️⃣7️⃣ What is Spliterator?
Supports parallel traversal of collections.
4️⃣8️⃣ What is Stream API’s relation to collections?
Streams process collections in a functional style.
4️⃣9️⃣ Why Map is not part of Collection interface?
Because Map works with key–value pairs, not single elements.
5️⃣0️⃣ When to use which collection?
| Scenario | Use |
|---|---|
| Fast lookup | HashMap |
| Ordered data | LinkedHashMap |
| Sorted data | TreeMap |
| Thread safety | ConcurrentHashMap |
🎯 Interview Final Tip
Most collection questions test internal working and trade-offs, not just definitions.
No comments:
Post a Comment