Java Map Hierarchy

Table of contents

πΊοΈ Java Map Hierarchy β HashMap, TreeMap, LinkedHashMap Explained
β¨ Introduction
In Java, the Map
interface is part of the Java Collections Framework, but unlike List
, Set
, and Queue
, it doesnβt extend the Collection interface.
The Map
stores key-value pairs, where each key is unique, and each value is associated with a key. Itβs one of the most commonly used structures in real-world projects and interviews.
π§ Map Interface Hierarchy
Hereβs a simple overview of the core Map implementations:
Map
βββ HashMap
βββ LinkedHashMap
βββ TreeMap
βββ Hashtable
Each implementation offers different performance, ordering, and null-handling characteristics.
π§± Key Implementations of Map
1οΈβ£ HashMap
Unordered β no guarantee on the iteration order
Allows
null
keys and multiplenull
valuesNon-synchronized
Backed by a hash table
π‘ When to Use:
When you need fast access, and ordering doesnβt matter.
Map<String, String> map = new HashMap<>();
map.put("A", "Apple");
map.put("B", "Banana");
βοΈ Big O:
Operation | Time Complexity |
put/get/remove | O(1) average, O(n) worst |
containsKey | O(1) average |
2οΈβ£ LinkedHashMap
Maintains insertion order
Slightly slower than
HashMap
Still allows
null
keys and values
π‘ When to Use:
When you want fast lookups and predictable iteration order.
Map<String, String> map = new LinkedHashMap<>();
map.put("B", "Banana");
map.put("A", "Apple");
3οΈβ£ TreeMap
Sorted based on the key (natural order or comparator)
No
null
keys allowed (but null values are allowed)Backed by a Red-Black tree
π‘ When to Use:
When you need a sorted map or range queries.
Map<Integer, String> map = new TreeMap<>();
map.put(3, "Three");
map.put(1, "One");
βοΈ Big O:
Operation | Time Complexity |
put/get/remove | O(log n) |
4οΈβ£ Hashtable
Legacy class (before Java Collections Framework)
Thread-safe, but slower
No
null
keys or values allowed
β οΈ Recommendation:
Avoid in new code; use ConcurrentHashMap
if you need thread safety.
π Map vs Collection
Feature | Map | Collection |
Key-Value Pair | β Yes | β No |
Duplicates | β Keys must be unique | β Duplicates allowed (List) |
Extends Collection? | β No | β Yes (List, Set, etc.) |
π Synchronized Variants
For thread-safe operations:
Map<String, String> syncMap = Collections.synchronizedMap(new HashMap<>());
Or use
ConcurrentHashMap
for better performance in multi-threaded environments.
β Summary
Map Type | Ordering | Null Keys | Thread Safe | Sorted |
HashMap | No | Yes | No | No |
LinkedHashMap | Insertion order | Yes | No | No |
TreeMap | Sorted order | No | No | Yes |
Hashtable | No | No | Yes | No |
π Final Thoughts
Understanding the Map hierarchy is crucial for writing efficient and readable Java code. Each implementation serves a unique use case:
π Use
HashMap
for general-purpose lookupsπ Use
LinkedHashMap
when order mattersπ’ Use
TreeMap
when sorted keys are requiredπ§΅ Use
ConcurrentHashMap
for thread-safe performance
Subscribe to my newsletter
Read articles from Mahock Abraham directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
