LinkedHashMap
LinkedHashMap is a HashMap that also keeps a doubly linked list of entries. Iteration follows insertion order, or access order if you turn that mode on.
Detailed Explanation
HashMap iteration jumps by bucket. That order surprises users and breaks tests that expect "first key I put is first key I see". LinkedHashMap adds a linked list through the entries. By default the list is insertion order: new keys append, and replacing a value does not move the entry. eldest is the first inserted key still in the map.
Access-order mode (new LinkedHashMap<>(16, 0.75f, true)) moves an entry to the tail on get and put. The head is then the least recently used entry. Override removeEldestEntry to return true when the map should drop that head, and you have a small LRU cache. The JDK documents this pattern; it is not a full cache (no expiry, no concurrency).
Lookup cost stays amortized O(1). You pay an extra two pointers per entry and a little work to stitch the list. That is cheap relative to TreeMap's O(log n) and far cheaper than sorting keys on every print.
You reach for LinkedHashMap when you need a map and a stable iteration order: request parameter maps, JSON objects you want to pretty-print in the order they were built, or a replay of inserts. You reach for access order when you want a bounded cache in one thread. You keep HashMap when order does not matter and you want the slightly smaller footprint. You use ConcurrentHashMap when threads share the map; there is no concurrent LinkedHashMap in the JDK.
The cost of using it as a cache without removeEldestEntry is an unbounded map. The cost of access order is that a read is a structural change: it throws ConcurrentModificationException if another iterator is walking the map, even in one thread, if you get during a for-each. Do not iterate and get the same map in access-order mode.
LinkedHashMap allows one null key and null values, like HashMap. It is not thread-safe. sequenced methods in Java 21 (firstEntry, pollFirstEntry) make the ends of the list explicit.
Code Examples
import java.util.LinkedHashMap;
import java.util.Map;
public class InsertOrder {
public static void main(String[] args) {
Map<String, Integer> ages = new LinkedHashMap<>();
ages.put("cy", 9);
ages.put("ana", 12);
ages.put("bo", 10);
System.out.println(ages.keySet()); // prints: [cy, ana, bo]
}
}import java.util.LinkedHashMap;
import java.util.Map;
public class Lru {
static LinkedHashMap<String, String> cache(int max) {
return new LinkedHashMap<>(16, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
return size() > max;
}
};
}
public static void main(String[] args) {
Map<String, String> cache = cache(2);
cache.put("a", "1");
cache.put("b", "2");
cache.get("a"); // a is now most recently used
cache.put("c", "3"); // b is eldest and is dropped
System.out.println(cache.keySet()); // prints: [a, c]
}
}Common Gotchas
Access-order `get` is a structural modification
iterating and calling `get` on the same map can throw `ConcurrentModificationException`.
`put` of an existing key does not change insertion order
only access-order mode moves the entry.
An LRU `LinkedHashMap` is not thread-safe and has no TTL
use a real cache if you need either.
There is no concurrent linked hash map in the JDK
wrap or pick another structure.