LinkedHashMap and TreeMap are two important Map implementations in Java that extend the capabilities of HashMap by providing ordering guarantees.
They are part of:
java.utilBoth store data as key–value pairs, but they differ in:
- ordering behavior
- internal data structure
- performance
- use cases
These maps are frequently used in backend systems, especially in caching, LRU design, sorting data, and ordered API responses.
LinkedHashMap is a HashMap with a doubly linked list running through the entries.
It maintains:
- Insertion order by default
- Optional access order (used in LRU cache)
Defined as:
class LinkedHashMap<K,V> extends HashMap<K,V>| Feature | Behavior |
|---|---|
| Ordering | Maintains insertion order (or access order) |
| Null keys/values | Allowed (one null key, multiple null values) |
| Performance | O(1) average for operations |
| Internal structure | HashMap + doubly linked list |
It internally uses:
- HashMap for storage
- Doubly linked list for order tracking
Each entry node contains:
Entry<K,V> before;
Entry<K,V> after;This preserves order during iteration.
Map<String, Integer> map = new LinkedHashMap<>();
map.put("Java", 1);
map.put("Python", 2);
map.put("C++", 3);
System.out.println(map);Output:
{Java=1, Python=2, C++=3}
Order is preserved.
LinkedHashMap can maintain access order instead of insertion order:
Map<Integer, String> lru = new LinkedHashMap<>(16, 0.75f, true);Now most recently accessed entry moves to the end.
This is used to implement LRU Cache.
class LRUCache<K,V> extends LinkedHashMap<K,V> {
private final int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return size() > capacity;
}
}Usage:
LRUCache<Integer, String> cache = new LRUCache<>(3);
cache.put(1, "A");
cache.put(2, "B");
cache.put(3, "C");
cache.get(1);
cache.put(4, "D"); // removes least recently usedThis is used in:
- API rate limiters
- session cache
- database query caching
- token stores
TreeMap stores keys in sorted order.
Sorting is based on:
- natural ordering
- or custom Comparator
Internally uses:
- Red-Black Tree
Defined as:
class TreeMap<K,V> implements NavigableMap<K,V>| Feature | Behavior |
|---|---|
| Ordering | Sorted (ascending by default) |
| Null key | Not allowed |
| Null values | Allowed |
| Internal structure | Red-Black Tree |
| Performance | O(log n) |
Map<Integer, String> map = new TreeMap<>();
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
System.out.println(map);Output:
{1=A, 2=B, 3=C}
Map<String, Integer> map =
new TreeMap<>(Comparator.reverseOrder());
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);Order becomes descending:
{Cherry=3, Banana=2, Apple=1}
TreeMap<Integer, String> leaderboard = new TreeMap<>();
leaderboard.put(90, "John");
leaderboard.put(95, "Emma");
leaderboard.put(85, "Liam");We can fetch:
- Top scorer
- Students in score range
- Next higher score
leaderboard.lastEntry(); // highest score
leaderboard.firstEntry(); // lowest score
leaderboard.subMap(80, 95);Used in:
- ranking systems
- financial order books
- scheduling
- priority ranking services
| Operation | HashMap | LinkedHashMap | TreeMap |
|---|---|---|---|
| get/put/remove | O(1) avg | O(1) avg | O(log n) |
| Maintains order? | No | Yes | Yes (sorted) |
| Null keys | 1 allowed | 1 allowed | Not allowed |
| Underlying structure | Hash table | Hash table + linked list | Red-black tree |
- order of keys matters
- predictable iteration required
- implementing LRU cache
- preserving insertion order in API responses
- you need sorted keys
- range queries are required
- floor/ceiling navigation required
TreeMap supports navigation methods like:
higherKey()
lowerKey()
ceilingEntry()
floorEntry()
subMap()
headMap()
tailMap()
- hash table + doubly linked list
- O(1) operations
- preserves order
- self-balancing red-black tree
- O(log n) operations
- keeps sorted order
- LinkedHashMap maintains insertion/access order
- TreeMap maintains sorted order
- TreeMap gives O(log n)
- LinkedHashMap gives O(1)
No — because sorting requires comparison.
LinkedHashMap (access-order mode).
HashMap ≥ LinkedHashMap >> TreeMap
Because it uses Red-Black Tree rotations during insert, delete, lookup.
-
LinkedHashMap preserves order of insertion or access.
-
TreeMap stores keys in sorted order.
-
LinkedHashMap suitable for caches.
-
TreeMap suitable for range queries and sorted data.
-
TreeMap operations are O(log n), LinkedHashMap are O(1).
-
Both are widely used in backend systems.