Java Reference ยท Collections
The LinkedHashMap class
Reference for java.util.LinkedHashMap: predictable iteration order, access-order mode, removeEldestEntry for LRU caches, and sequenced-map methods.
LinkedHashMap<K, V> is a HashMap that also keeps its entries in a doubly linked list. Lookups stay expected O(1), and iteration follows a predictable order: by default the order in which keys were first inserted. Re-putting an existing key does not move it.
A second mode, access order, moves an entry to the end every time it is read or written. Combined with the protected removeEldestEntry() hook, that gives you a small least-recently-used (LRU) cache in a few lines.
Map<String, Integer> ordered = new LinkedHashMap<>(); Map<String, Integer> lru = new LinkedHashMap<>(16, 0.75f, true); // access order
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class LinkedHashMap<K, V> extends HashMap<K, V> implements SequencedMap<K, V>
| Constructor | Description |
|---|---|
LinkedHashMap() |
Insertion-ordered map. |
LinkedHashMap(int initialCapacity) |
Also sets capacity. |
LinkedHashMap(int initialCapacity, float loadFactor) |
Also sets load factor. |
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) |
true selects access order. |
LinkedHashMap(Map<? extends K, ? extends V> m) |
Copy in m's iteration order. |
| Member | Description |
|---|---|
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) |
Called after put/putIfAbsent/merge-style insertions; return true to remove the eldest entry. |
firstEntry(), lastEntry(), pollFirstEntry(), pollLastEntry() |
Ends of the map (Java 21). |
putFirst(K, V), putLast(K, V) |
Insert or move to an end (Java 21). |
reversed(), sequencedKeySet(), sequencedValues(), sequencedEntrySet() |
Reverse and sequenced views (Java 21). |
static newLinkedHashMap(int n) |
Pre-sized map (Java 19). |
Usage notes
- Cost: a little more memory and slightly slower updates than
HashMap; lookups are stillO(1). - In access order mode,
get()is a structural change. Iterating while callingget()on the same map throwsConcurrentModificationException. removeEldestEntrymust not modify the map itself; it only returns a decision. For a thread-safe cache wrap withCollections.synchronizedMapor use a cache library.- Order of views (
keySet,values,entrySet) follows the map order. - Reinsertion: in insertion-order mode, putting an existing key keeps its position; remove it first to move it to the end.
- Equality ignores order: a
LinkedHashMapequals aHashMapwith the same entries. - Not synchronized, and nulls are allowed like in
HashMap.