Sub topic 10
Sequenced Map Views: Keys, Values, and Entries
This chapter assumes you know Java Map, generics, interfaces, and LinkedHashMap. It also assumes Java 21 or newer. A sequenced map is a map that has a defined encounter order, such as insertion order.
SequencedMap provides three ordered views of a map:
sequencedKeySet()returns the keys as aSequencedSet.sequencedValues()returns the values as aSequencedCollection.sequencedEntrySet()returns map entries as aSequencedSet.
These views are connected to the original map. They are not independent copies.
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SequencedCollection;
import java.util.SequencedMap;
import java.util.SequencedSet;
public class MapViews {
public static void main(String[] args) {
SequencedMap<String, Integer> scores = new LinkedHashMap<>();
scores.put("Ada", 95);
scores.put("Linus", 88);
scores.put("Grace", 91);
SequencedSet<String> keys = scores.sequencedKeySet();
SequencedCollection<Integer> values = scores.sequencedValues();
SequencedSet<Map.Entry<String, Integer>> entries =
scores.sequencedEntrySet();
System.out.println(keys); // [Ada, Linus, Grace]
System.out.println(values); // [95, 88, 91]
System.out.println(entries); // [Ada=95, Linus=88, Grace=91]
System.out.println(keys.getFirst()); // Ada
System.out.println(values.getLast()); // 91
System.out.println(entries.reversed());
// [Grace=91, Linus=88, Ada=95]
}
}The views share the map's order
The key, value, and entry views use the map's encounter order. For a LinkedHashMap, this is normally insertion order. The views also have their own reversed() methods.
Because each view is live, changes in the map appear in the views:
import java.util.LinkedHashMap;
import java.util.SequencedMap;
public class LiveViews {
public static void main(String[] args) {
SequencedMap<String, Integer> map = new LinkedHashMap<>();
map.put("red", 1);
map.put("blue", 2);
var keys = map.sequencedKeySet();
var values = map.sequencedValues();
map.put("green", 3);
System.out.println(keys); // [red, blue, green]
System.out.println(values); // [1, 2, 3]
keys.remove("blue");
System.out.println(map); // {red=1, green=3}
}
}Removing an element through a view removes the related mapping. Removing a key removes its key-value pair. Removing a value removes a matching mapping according to the view's iterator behavior. Removing an entry removes that entry from the map.
Choosing the correct view
Use sequencedKeySet() when you need ordered keys. It is useful for reading or removing the first or last key:
String oldestName = map.sequencedKeySet().getFirst();
String newestName = map.sequencedKeySet().getLast();Use sequencedValues() when you need values in map order. Unlike keys and entries, values are allowed to repeat, so this view is a SequencedCollection, not a SequencedSet.
Use sequencedEntrySet() when you need both the key and value while preserving order:
for (var entry : map.sequencedEntrySet().reversed()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}The entry view is often the clearest choice when processing a map from newest to oldest.
Mutation rules and method types
The views expose end operations, but support depends on the underlying map.
getFirst()andgetLast()read the ends.removeFirst()andremoveLast()remove mappings through the view when supported.reversed()creates another live view.- Adding directly to a key or entry view is unsupported because a key or entry needs a map relationship.
- Adding directly to a values view is also unsupported because a value alone does not provide a key.
For a read-only API, accept the interface rather than LinkedHashMap:
import java.util.SequencedMap;
public class MapPrinter {
public static <K, V> void printNewestFirst(SequencedMap<K, V> map) {
for (var entry : map.sequencedEntrySet().reversed()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
}This method works with any compatible SequencedMap, while the caller decides which implementation to use.
Exercise
If a map contains {a=10, b=20, c=30}, what does map.sequencedKeySet().reversed() represent, and what happens if you remove "b" from that view?
Show answer
It represents the live key view [c, b, a]. Removing "b" also removes the b=20 mapping from the original map.