Java Collection Framework - Interview Study Guide
What is Java Collection Framework?
Java Collection Framework (JCF) is a set of classes and interfaces that implement commonly reusable
collection data structures. It provides algorithms to manipulate them such as searching, sorting, and shuffling.
Hierarchy of Java Collections
Core Interfaces:
- Collection: Root interface for List, Set, and Queue
- Map: Key-value pairs
Classes:
- List: ArrayList, LinkedList, Vector
- Set: HashSet, TreeSet, LinkedHashSet
- Queue: PriorityQueue, ArrayDeque
- Map: HashMap, TreeMap, LinkedHashMap
List Interface
List is an ordered collection that allows duplicates.
- ArrayList: Best for random access
- LinkedList: Best for insert/delete
- Vector: Synchronized, legacy
Set Interface
Set does not allow duplicates.
- HashSet: No order, backed by HashMap
- LinkedHashSet: Maintains insertion order
- TreeSet: Sorted, uses Red-Black Tree
Queue Interface
Page 1
Java Collection Framework - Interview Study Guide
Queue follows FIFO.
- PriorityQueue: Ordered by comparator or natural order
- ArrayDeque: Double-ended queue with no capacity restrictions
Map Interface
Map holds key-value pairs.
- HashMap: Allows one null key, fast access
- LinkedHashMap: Maintains insertion order
- TreeMap: Sorted keys
- ConcurrentHashMap: Thread-safe and high-performance
Collections Utility Class
Collections class provides utility methods such as:
- sort(), reverse(), shuffle()
- synchronizedList(), unmodifiableList()
Iterator and ListIterator
Iterator: Forward only, all collections
ListIterator: Bi-directional, only for List
Concurrent Collections
- ConcurrentHashMap: Segment-based concurrency
- CopyOnWriteArrayList: Safe iteration during modification
- BlockingQueue: Used for producer-consumer scenarios
Best Practices
- Prefer ArrayList for reads, LinkedList for inserts
- Use Set for uniqueness
Page 2
Java Collection Framework - Interview Study Guide
- Use ConcurrentHashMap in multithreaded environments
- Always override hashCode() and equals() for keys
Code Examples
Remove Duplicates:
Set<T> set = new LinkedHashSet<>(list);
Sort Map by Value:
map.entrySet().stream().sorted(Map.Entry.comparingByValue())
Custom Comparator:
Collections.sort(list, (a, b) -> b.age - a.age);
Common Interview Questions
- Why Set doesn't allow duplicates?
- How does HashMap handle collisions?
- Difference between HashMap, TreeMap, LinkedHashMap
- Why hashCode() and equals() matter?
- Fail-fast vs fail-safe
- Performance and thread-safety aspects
Recommended Resources
- Java Docs: https://docs.oracle.com/javase/8/docs/technotes/guides/collections/index.html
- GeeksforGeeks
- Java Brains YouTube
- Book: Effective Java by Joshua Bloch
Page 3