Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What ordering does LinkedHashMap keep, and what did Java 21's SequencedMap add to it?

By default LinkedHashMap keeps insertion order, and putting an existing key again does not move it. Created with accessOrder set to true, it keeps access order instead, from least to most recently used, which is the basis of an LRU cache. It does this with a doubly linked list running through its entries alongside the hash table, which costs two extra references per entry but makes iteration proportional to the size rather than the capacity. Java 21 made it a SequencedMap, adding firstEntry, lastEntry, pollFirstEntry, pollLastEntry, putFirst, putLast and reversed.

Why this question is asked

Whenever the order of a map matters, in JSON output, in a configuration list, in a report or in a cache, LinkedHashMap is usually the answer. It behaves exactly like a HashMap for lookups and adds a predictable iteration order on top. Interviewers ask about it to check that we know the two orders it supports, what they cost, and how the newer SequencedMap interface finally gave it a direct way to reach its first and last entries. In this answer, we will recap why HashMap has no order, see how LinkedHashMap adds one, compare insertion order with access order, look at the cost, and then use the Java 21 methods.

1A Quick Recap: Why HashMap Has No Order

A HashMap stores entries in buckets chosen from each key's hash, and iteration walks the buckets from first to last. So the order we see depends on hash values and table size, not on when we added each key, and it can change after a resize. The Javadoc makes no promise about it.

LinkedHashMap extends HashMap and keeps the same buckets for lookups, so get() and put() are still O(1) on average. In addition, every entry has two extra references, before and after, that link all entries into one doubly linked list. Iteration follows that list instead of the buckets.

2Insertion Order, the Default

By default, the list is in insertion order: a new key goes to the end. Putting a key that is already present only replaces its value; it does not move it.

Java
Map<String, Integer> m = new LinkedHashMap<>();
m.put("b", 1);
m.put("a", 2);
m.put("c", 3);
m.put("b", 9); // existing key: value replaced, position kept
System.out.println(m); // {b=9, a=2, c=3}
m.remove("a");
m.put("a", 4); // removed and added again: now a new entry at the end
System.out.println(m); // {b=9, c=3, a=4}

Notice the difference between the two cases. Re-putting keeps the original position, while removing and re-adding creates a new entry at the end. This is the order we want for things like preserving the field order of a parsed document.

3Access Order, for Caches

The constructor LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) with accessOrder set to true changes the rule: every access moves the entry to the end of the list. Accesses include get(), put() of an existing key, getOrDefault(), and compute-style methods. The front of the list is therefore the least recently used entry and the end is the most recently used.

Java
Map<String, Integer> lru = new LinkedHashMap<>(16, 0.75f, true);
lru.put("a", 1); lru.put("b", 2); lru.put("c", 3);
lru.get("a"); // a is now the most recently used
System.out.println(lru); // {b=2, c=3, a=1}
lru.put("b", 20); // updating also counts as an access
System.out.println(lru); // {c=3, a=1, b=20}
The same three puts and one get, in each mode
StepInsertion orderAccess order
put a, b, ca, b, ca, b, c
get(a)a, b, cb, c, a
put(b, 20) on existing ba, b, cc, a, b
remove(a), put(a)b, c, ac, b, a
Front of the list on the left; in access order the front is the eviction candidate.

Combined with overriding removeEldestEntry() to evict the front entry when the map is too big, this is a complete LRU cache in a few lines. But there is a catch: in access-order mode even get() changes the list, so it counts as a structural modification. Iterating the map while calling get() throws ConcurrentModificationException, and concurrent readers need a lock.

4What the Order Costs

Each LinkedHashMap entry is a HashMap node plus two references, so on a 64-bit JVM with compressed references it is about 40 bytes instead of 32, and every insert and remove updates two more links. In return, iteration only visits real entries. A HashMap's iterator must scan every bucket of its table, including empty ones, so iterating a HashMap with a capacity of a million and ten entries scans a million slots, while a LinkedHashMap visits ten entries. So iteration cost is proportional to size for LinkedHashMap and to capacity plus size for HashMap.

5What Java 21's SequencedMap Added

Before Java 21, getting the last entry of a LinkedHashMap meant iterating through all of it. Java 21 introduced the sequenced collection interfaces, and LinkedHashMap now implements SequencedMap, which gives direct, named access to both ends.

Java
LinkedHashMap<String, Integer> m = new LinkedHashMap<>();
m.put("b", 1); m.put("a", 2); m.put("c", 3);
System.out.println(m.firstEntry()); // b=1
System.out.println(m.lastEntry()); // c=3
System.out.println(m.reversed()); // {c=3, a=2, b=1}: a reverse-ordered view
m.putFirst("z", 0); // insert at the front
System.out.println(m); // {z=0, b=1, a=2, c=3}
m.putLast("b", 5); // existing key: moved to the end, value replaced
System.out.println(m); // {z=0, a=2, c=3, b=5}
System.out.println(m.pollFirstEntry()); // z=0, and removes it
System.out.println(m.sequencedKeySet().reversed()); // [b, c, a]
Step throughWhere LinkedHashMap sits after Java 21
  1. Mapget, put, remove
  2. SequencedMap (Java 21)firstEntry, lastEntry, pollFirstEntry, pollLastEntry, putFirst, putLast, reversed
  3. HashMapbuckets for O(1) lookups
  4. LinkedHashMapplus a doubly linked list: insertion or access order

4 layers, from the top down. Step through them one at a time.

Notice that putFirst and putLast deliberately move an existing key, unlike plain put. reversed() returns a view, not a copy, so changes through it affect the original. For an access-ordered map, the exact effect of putFirst and putLast is described in the Javadoc for your version. TreeMap and the other sorted maps implement SequencedMap too, through NavigableMap, which now shares these method names.

6Follow-up questions

Does LinkedHashMap make iteration order stable across JDK versions?

Yes, that is the point. The order is defined by the specification, insertion or access order, not by hash values, so it does not change with the JDK, the table size or the keys' hash codes.

Is LinkedHashMap thread-safe?

No, just like HashMap. In access-order mode it is even more fragile, since reads modify the list. For concurrent use we either wrap it with a lock or use a concurrent cache library.

What is the difference between SequencedMap and NavigableMap?

SequencedMap is about encounter order, whatever defines it, insertion or sorting. NavigableMap is about sorted order and adds searches such as floorKey or ceilingEntry. A TreeMap is both; a LinkedHashMap is only sequenced.

7Conclusion

In this answer, we saw that LinkedHashMap keeps insertion order by default, where re-putting an existing key keeps its position, and access order when created with accessOrder true, from least to most recently accessed, which is what an LRU cache needs. It achieves this with a doubly linked list through its entries, costing two extra references per entry but making iteration proportional to size rather than capacity. Java 21 made it a SequencedMap with firstEntry, lastEntry, pollFirstEntry, pollLastEntry, putFirst, putLast and reversed.

What to say in the interviewAbout 90 seconds aloud

Open with

LinkedHashMap is a HashMap that also links its entries into a doubly linked list, so it has a defined iteration order. By default that is insertion order: new keys go to the end, and putting an existing key again only replaces the value, it does not move it.

Then, if they want more

If I create it with the three-argument constructor and pass true for accessOrder, it keeps access order instead: every get, or put of an existing key, moves that entry to the end, so the front is the least recently used entry. That is what makes it easy to build an LRU cache with removeEldestEntry.

The catch is that in access-order mode a get modifies the list, so it is a structural change and not safe for concurrent readers.

The cost of the list is two extra references per entry, so roughly 40 bytes per entry instead of 32, but iteration is proportional to the number of entries instead of the table capacity, unlike HashMap, which has to scan empty buckets too. Java 21 added sequenced collections, and LinkedHashMap now implements SequencedMap.

That gives firstEntry and lastEntry, pollFirstEntry and pollLastEntry to remove from either end, putFirst and putLast, which place or move a key to the front or the end, and reversed, which returns a reverse-ordered view. Before that, getting the last entry meant iterating the whole map.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What ordering does LinkedHashMap keep, and what did Java 21's SequencedMap add to it?": https://scrivio-interview-library.netlify.app/java/hashmap-internals/3e0ddb5423/