Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java 21 access-order LinkedHashMap: reads change a reversed view

Last updated: 5 Oct 20264 min read
tutorial
IntermediateBy AITrove Editorial

An access-order LinkedHashMap moves a successfully accessed mapping toward its backing tail, so a reversed view reports that mapping first.

Make the ordering policy visible

The third LinkedHashMap constructor argument selects access order. A successful get changes encounter order even though no mapping is inserted or removed. If a reverse-ordered view is already held, its first key can change after that read. A renderer that assumes reads are observational can produce different output after a cache lookup.

The fixture prints both orientations before and after accessing the oldest key. The mapping count stays fixed. The order change comes from the chosen map policy, not from copying or rebuilding the reverse view.

Separate explicit placement from access

putFirst and putLast reposition an entry because the caller requested an endpoint. Access order repositions an entry as a side effect of operations such as get. Use one policy deliberately; a cache and a stable report have different needs.

Iteration over either view while another thread accesses the map needs a concurrency rule. Access-order movement can count as structural change for iterator behavior, so fail-fast exceptions remain diagnostics rather than coordination.

Working program

Java
import java.util.LinkedHashMap;
import java.util.SequencedMap;

public class AccessOrderedDispatchCache {
    public static void main(String[] args) {
        LinkedHashMap<String, Integer> cache = new LinkedHashMap<>(16, 0.75f, true);
        cache.put("load-15", 1);
        cache.put("seal-47", 2);
        cache.put("ship-82", 3);
        SequencedMap<String, Integer> reverse = cache.reversed();
        System.out.println(reverse.sequencedKeySet());
        System.out.println(cache.get("load-15"));
        System.out.println(cache.keySet());
        System.out.println(reverse.sequencedKeySet());
    }
}

Output

Output
[ship-82, seal-47, load-15]
1
[seal-47, ship-82, load-15]
[load-15, ship-82, seal-47]

Cost and ownership

The view does not copy map entries. A successful lookup is expected constant-time under ordinary hash distribution but also changes ordering in this map mode. Traversing either orientation is O(n), and both references share the same mutable mappings.

Common Mistakes

  • Do not assume get leaves an access-order map's iteration order unchanged.
  • Do not treat a reversed view as a frozen cache report.
  • Do not rely on fail-fast iteration to make concurrent access safe.

Read next

Java LinkedHashMap: encounter order and a bounded cache model, Java 21 SequencedMap.reversed: edits through a reverse-ordered map view, Java 21 LinkedHashMap.putFirst and putLast for explicit key order, Java LinkedList fail-fast iterators: a bug signal, not safety.

java
collections
linkedhashmap-access-order-reversal
Storage details