If you're working with Java, you've likely used collections—ArrayList, HashSet, or HashMap. But have you ever wondered how these classes are structured or related? That’s where understanding the Java Collection Framework hierarchy becomes crucial.
Think of the framework as a family tree of data structures, designed for storing, processing, and retrieving objects efficiently. It helps developers choose the right data structure for the task, boosting performance, readability, and maintainability.
🌳 Core Definition and Purpose
The Java Collections Framework (JCF) is a group of interfaces, abstract classes, and concrete classes designed to store and manipulate groups of data.
- Interfaces: Define behavior (List, Set, Map,
Queue
) - Abstract Classes: Provide partial implementations (
AbstractList
,AbstractSet
) - Concrete Classes: Full implementations ready to use (
ArrayList
,HashSet
,TreeMap
)
🧬 Java Collection Framework Hierarchy
High-Level Overview
Iterable
|
Collection
/ | \
List Set Queue
| | |
ArrayList HashSet PriorityQueue
Map Is Special
Map
is not a subtype of Collection
. It’s a parallel hierarchy for key–value data.
Map
/ | \
HashMap TreeMap LinkedHashMap
🔍 Interface Breakdown
Collection Interface
The root of the JCF, extended by List
, Set
, and Queue
.
List
- Ordered, allows duplicates
- Implementations:
ArrayList
,LinkedList
Set
- No duplicates, unordered or ordered (via
TreeSet
,LinkedHashSet
) - Implementations:
HashSet
,TreeSet
,LinkedHashSet
Queue
- Supports FIFO operations
- Implementations:
PriorityQueue
,ArrayDeque
Map
- Stores key-value pairs
- Implementations:
HashMap
,TreeMap
,LinkedHashMap
💻 Syntax and Class Examples
List Example
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Set Example
Set<String> set = new HashSet<>();
set.add("One");
set.add("One"); // Duplicate, ignored
Map Example
Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
⚙️ Internal Working & Performance
ArrayList Resizing
- Backed by an array
- Grows by ~50% when full
HashMap Hashing
- Uses
hashCode()
andequals()
- Collisions handled via chaining and treeification
Performance Table
Structure | Lookup | Insert | Delete |
---|---|---|---|
ArrayList | O(1)* | O(1)* | O(n) |
LinkedList | O(n) | O(1) | O(1) |
HashMap | O(1)* | O(1)* | O(1)* |
TreeMap | O(log n) | O(log n) | O(log n) |
* Amortized time
🔄 Functional Programming Support
Stream Example
List<String> result = list.stream()
.filter(e -> e.startsWith("A"))
.map(String::toUpperCase)
.collect(Collectors.toList());
Predicate & Function
Predicate<String> startsWithA = s -> s.startsWith("A");
Function<String, Integer> length = String::length;
🔁 Comparisons
Feature | List | Set | Map |
---|---|---|---|
Duplicates | Allowed | Not Allowed | Keys not allowed |
Order | Preserved | Depends | Based on implementation |
Null Support | Yes | Yes | Key/value varies |
🚨 Anti-patterns and Misuse
- Using
Vector
orHashtable
in new code - Forgetting
equals()
/hashCode()
overrides in key objects - Modifying collections while iterating
🧼 Refactoring Legacy Code
Before:
Hashtable<String, Integer> table = new Hashtable<>();
After:
Map<String, Integer> table = new HashMap<>();
✅ Modern, fast, thread-safe when needed via ConcurrentHashMap
.
✅ Best Practices
- Program to interfaces, not implementations
- Use generics (
List<String>
notList
) - Initialize with appropriate capacity
- Prefer
Collections.unmodifiableList()
when immutability is needed
📌 What's New in Java?
Java 8
- Streams API
- Lambdas
- Functional interfaces
Java 9
List.of()
,Map.of()
for immutable collections
Java 10+
var
for local variable type inference
Java 21
- Record patterns, enhanced performance
- Incubator for sequenced collections
📘 Conclusion and Key Takeaways
- Java Collections are organized into a flexible hierarchy
- Understanding the hierarchy helps in making better data structure choices
- Java 8+ features make collections more expressive and functional
- Keep performance, immutability, and maintainability in mind
FAQ – Collection Framework Hierarchy
-
Why is Map not a part of Collection?
Because it doesn't fit the single-element model. It stores key–value pairs. -
Which collection maintains order and allows duplicates?
ArrayList
andLinkedList
. -
What’s the difference between Set and List?
List allows duplicates; Set doesn’t. -
How is HashMap different from TreeMap?
HashMap is unordered and faster; TreeMap is sorted. -
Can I sort a HashSet?
Not directly. Convert it to aList
or useTreeSet
. -
What is the initial capacity of HashMap?
16, with load factor 0.75. -
Is LinkedList faster than ArrayList?
For frequent inserts/deletes, yes. For random access, no. -
Which is thread-safe: HashMap or ConcurrentHashMap?
OnlyConcurrentHashMap
is thread-safe. -
Can a Map have duplicate values?
Yes, but not duplicate keys. -
When to use LinkedHashMap?
When you need predictable iteration order.