Sub topic 6
SequencedMap and LinkedHashMap
This chapter assumes you know Map, LinkedHashMap, generics, interfaces, and the difference between insertion order and sorted order. The examples require Java 21 or newer.
Why `SequencedMap` matters
Before Java 21, Map did not provide a common API for the first and last entries. With a LinkedHashMap, you might write:
Map.Entry<String, Integer> first =
map.entrySet().iterator().next();Finding the last entry was less direct. You had to iterate through every entry, convert the entry set to a list, or use another collection. Java 21 adds SequencedMap, which gives maps with a defined encounter order a consistent API.
A SequencedMap<K, V> supports these operations:
| Operation | Purpose |
|---|---|
| `firstEntry()` | Read the first entry |
| `lastEntry()` | Read the last entry |
| `putFirst(key, value)` | Insert or move an entry to the front |
| `putLast(key, value)` | Insert or move an entry to the end |
| `pollFirstEntry()` | Remove and return the first entry |
| `pollLastEntry()` | Remove and return the last entry |
| `reversed()` | Obtain a live reverse-order view |
LinkedHashMap implements SequencedMap, so existing linked hash maps can use this API.
Basic `LinkedHashMap` operations
A LinkedHashMap normally preserves insertion order. The following map stores entries in the order one, two, then three.
import java.util.LinkedHashMap;
import java.util.SequencedMap;
public class MapEnds {
public static void main(String[] args) {
SequencedMap<String, Integer> numbers = new LinkedHashMap<>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);
System.out.println(numbers.firstEntry()); // one=1
System.out.println(numbers.lastEntry()); // three=3
numbers.putFirst("zero", 0);
numbers.putLast("four", 4);
System.out.println(numbers);
// {zero=0, one=1, two=2, three=3, four=4}
System.out.println(numbers.pollLastEntry()); // four=4
System.out.println(numbers);
// {zero=0, one=1, two=2, three=3}
}
}firstEntry() and lastEntry() return null when the map is empty. The polling methods also return null when there is no entry to remove.
Reversed map views
reversed() returns a live view. It does not copy the map. The view presents the original map's entries in the opposite encounter order.
import java.util.LinkedHashMap;
import java.util.SequencedMap;
public class ReversedMap {
public static void main(String[] args) {
SequencedMap<String, Integer> map = new LinkedHashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
SequencedMap<String, Integer> reverse = map.reversed();
System.out.println(reverse); // {c=3, b=2, a=1}
reverse.putFirst("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
map.putLast("e", 5);
System.out.println(reverse); // {e=5, d=4, c=3, b=2, a=1}
}
}An operation on the reverse view has the corresponding meaning from that view's direction. reverse.putFirst places the entry at the original map's end. This is useful for algorithms that need to process a map from newest to oldest without copying it.
Sequenced map views
A SequencedMap exposes ordered views:
sequencedKeySet()returns keys in encounter order.sequencedValues()returns values in encounter order.sequencedEntrySet()returns entries in encounter order.
Each view also has a reversed() method. For example:
for (String key : map.sequencedKeySet().reversed()) {
System.out.println(key);
}This avoids manually copying keys or reversing a list.
Empty, immutable, and unsupported maps
An empty sequenced map returns null from firstEntry, lastEntry, and the polling methods. A read-only wrapper can still expose the ordering operations, but methods that change order, such as putFirst or pollLastEntry, throw UnsupportedOperationException.
The interface describes the available operation. The implementation determines whether mutation is supported.
Exercise
A map contains entries in the order A, B, C. What does map.reversed().pollFirstEntry() remove?
Show answer
It removes C, because C is first in the reversed view and last in the original map.