Java Ordered Map
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, “ordered map” does not refer to one single class. Usually it means a Map implementation whose iteration order is predictable, and the correct choice depends on what “ordered” means for your use case: insertion order, access order, key-sorted order, or concurrent sorted order.
Why the Term Is Ambiguous
A plain HashMap does not guarantee iteration order. If the code depends on any stable order, you need a map implementation that explicitly defines one.
In practice, Java developers usually mean one of these:
- '
LinkedHashMapfor insertion order' - access-ordered
LinkedHashMapfor cache-like behavior - '
TreeMapfor key-sorted order' - '
ConcurrentSkipListMapwhen you need sorted keys and concurrent access'
Those are different guarantees. Using the wrong one often creates subtle bugs or unnecessary overhead.
LinkedHashMap for Insertion Order
If you want entries to come back in the same order they were inserted, use LinkedHashMap.
The iteration order is 3, 1, 2 because that is the insertion order.
This is useful when you need deterministic output but do not need sorted keys.
Access-Ordered LinkedHashMap
LinkedHashMap can also be configured to reorder entries when they are accessed. That is useful for simple least-recently-used cache patterns.
Now iteration reflects access history rather than original insertion order.
You can build a small LRU cache by overriding removeEldestEntry:
This is one of the most common reasons Java developers care about ordered maps.
TreeMap for Sorted Keys
If the keys themselves must stay sorted, use TreeMap.
This prints keys in sorted order: alice, bob, charlie.
TreeMap is also useful because it supports navigational operations such as:
- '
firstKey()' - '
lastKey()' - '
headMap()' - '
tailMap()' - '
subMap()'
If range queries matter, those methods are often more important than ordering alone.
Concurrent Sorted Order
If you need both sorted-key order and safe concurrent access, ConcurrentSkipListMap is worth knowing.
It is not a drop-in replacement for every map, but it is useful when ordering and concurrency both matter.
How to Choose
A practical rule is:
- use
LinkedHashMapif insertion order matters - use access-ordered
LinkedHashMapif recent access should influence order - use
TreeMapif keys must stay sorted - use
ConcurrentSkipListMapwhen you need sorted keys under concurrent access
That is the real question behind “ordered map”: what ordering rule do you actually need?
Common Pitfalls
The most common mistake is saying “ordered map” without specifying what order is required.
Another issue is relying on HashMap iteration order because it happened to look stable in tests. That is not a supported contract.
People also choose TreeMap when they really only needed insertion order, which adds logarithmic overhead for no benefit.
Finally, remember that TreeMap and ConcurrentSkipListMap need comparable keys or an explicit comparator. Without that, ordering cannot be defined correctly.
Summary
- In Java, “ordered map” can mean several different ordering guarantees.
- '
LinkedHashMappreserves insertion order by default.' - Access-ordered
LinkedHashMapis useful for cache-style behavior. - '
TreeMapkeeps keys sorted and supports range-style navigation.' - Choose the map based on the required ordering rule, not just the word “ordered.”

