TreeMap & LinkedHashMap
TreeMap & LinkedHashMap
You already know HashMap: fast, unordered. But sometimes you need more than fast lookups — you need the keys to come out in a predictable order. Java offers two Map implementations that solve this in different ways: TreeMap keeps keys sorted by their natural order (or a custom comparator), while LinkedHashMap preserves the order in which keys were inserted.
Why Order Matters
Consider building a leaderboard, a sorted dictionary, or a cache that evicts the oldest entry first. A plain HashMap offers no ordering guarantee at all — iteration can come out in any sequence. Choosing the right ordered map makes your intent explicit and eliminates fragile sorting steps after the fact.
TreeMap — Sorted by Key
TreeMap<K, V> is backed by a Red-Black tree. Every put and get is O(log n) rather than the amortised O(1) of a hash map, but in return the map stays sorted at all times.
Because the tree is always sorted, TreeMap exposes a rich set of navigation methods that plain maps lack:
TreeMap implements NavigableMap, which adds floorKey, ceilingKey, lowerKey, higherKey, headMap, tailMap, and subMap. Declare your variable as NavigableMap<K,V> when you want to expose these methods to callers.
Custom Sort Order with a Comparator
When you need a different sort order — say, reverse alphabetical, or case-insensitive — pass a Comparator to the constructor:
Comparable and provide no comparator, the first put will throw a ClassCastException at runtime.
LinkedHashMap — Insertion-Ordered
LinkedHashMap<K, V> wraps a regular hash table with a doubly-linked list that threads through every entry in insertion order. Lookups are still O(1) — just like HashMap — but iteration reflects the order keys were first added.
Access-Order Mode — Building an LRU Cache
There is a second constructor form: new LinkedHashMap<>(initialCapacity, loadFactor, true). The third argument switches the map from insertion-order to access-order: every get or put moves the accessed entry to the tail of the linked list. Combined with removeEldestEntry, this gives you a simple LRU (Least Recently Used) cache in about ten lines:
Collections.synchronizedMap or use a dedicated library.
Choosing Between the Three Map Implementations
- HashMap — fastest (O(1) amortised), no ordering guarantee. Use when order does not matter.
- LinkedHashMap — O(1) lookups, preserves insertion (or access) order. Use for predictable iteration, JSON serialisation order, or LRU caches.
- TreeMap — O(log n) operations, always sorted. Use when you need range queries, navigation methods, or guaranteed sorted output.
Quick Comparison
Map<K, V> unless you specifically need NavigableMap methods. This keeps your code flexible — you can swap the implementation without touching call sites.
Summary
TreeMap keeps keys sorted (natural order or a custom Comparator) at the cost of O(log n) operations, and adds powerful navigation methods like floorKey, ceilingKey, and subMap. LinkedHashMap preserves insertion order (or access order when that flag is set) at O(1) speed — making it ideal for predictable iteration and LRU caches. Pick based on whether you need sorted navigation, insertion order, or raw speed.