Java : SequencedMap with Examples

SequencedMap (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SequencedMap methods.


Summary

A Map that has a well-defined encounter order, that supports operations at both ends, and that is reversible. The encounter order of a SequencedMap is similar to that of the elements of a SequencedCollection, but the ordering applies to mappings instead of individual elements.

Class diagram

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.reversed()); // {c=30, b=20, a=10}

map.putFirst("X", 700);
map.putFirst("Y", 800);
map.putFirst("Z", 900);

System.out.println(map); // {Z=900, Y=800, X=700, a=10, b=20, c=30}
System.out.println(map.reversed()); // {c=30, b=20, a=10, X=700, Y=800, Z=900}

Methods

default Map.Entry<K,V> firstEntry ()

Returns the first key-value mapping in this map, or null if the map is empty.

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

System.out.println(map); // {}
System.out.println(map.firstEntry()); // null

map.putLast("a", 10);

System.out.println(map); // {a=10}
System.out.println(map.firstEntry()); // a=10

map.putLast("b", 20);

System.out.println(map); // {a=10, b=20}
System.out.println(map.firstEntry()); // a=10

map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.firstEntry()); // a=10

default Map.Entry<K,V> lastEntry ()

Returns the last key-value mapping in this map, or null if the map is empty.

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

System.out.println(map); // {}
System.out.println(map.lastEntry()); // null

map.putLast("a", 10);

System.out.println(map); // {a=10}
System.out.println(map.lastEntry()); // a=10

map.putLast("b", 20);

System.out.println(map); // {a=10, b=20}
System.out.println(map.lastEntry()); // b=20

map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.lastEntry()); // c=30

default Map.Entry<K,V> pollFirstEntry ()

Removes and returns the first key-value mapping in this map, or null if the map is empty (optional operation).

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.pollFirstEntry()); // a=10
System.out.println(map); // {b=20, c=30}

System.out.println(map.pollFirstEntry()); // b=20
System.out.println(map); // {c=30}

System.out.println(map.pollFirstEntry()); // c=30
System.out.println(map); // {}

System.out.println(map.pollFirstEntry()); // null

default Map.Entry<K,V> pollLastEntry ()

Removes and returns the last key-value mapping in this map, or null if the map is empty (optional operation).

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.pollLastEntry()); // c=30
System.out.println(map); // {a=10, b=20}

System.out.println(map.pollLastEntry()); // b=20
System.out.println(map); // {a=10}

System.out.println(map.pollLastEntry()); // a=10
System.out.println(map); // {}

System.out.println(map.pollLastEntry()); // null

default V putFirst (K k, V v)

Inserts the given mapping into the map if it is not already present, or replaces the value of a mapping if it is already present (optional operation).

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putFirst("a", 10);
System.out.println(map); // {a=10}

map.putFirst("b", 20);
System.out.println(map); // {b=20, a=10}

map.putFirst("c", 30);
System.out.println(map); // {c=30, b=20, a=10}

default V putLast (K k, V v)

Inserts the given mapping into the map if it is not already present, or replaces the value of a mapping if it is already present (optional operation).

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
System.out.println(map); // {a=10}

map.putLast("b", 20);
System.out.println(map); // {a=10, b=20}

map.putLast("c", 30);
System.out.println(map); // {a=10, b=20, c=30}

SequencedMap<K,V> reversed ()

Returns a reverse-ordered view of this map.

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

final var reversedMap = map.reversed();
System.out.println(reversedMap); // {c=30, b=20, a=10}

System.out.println(reversedMap.reversed()); // {a=10, b=20, c=30}

default SequencedSet<Map.Entry<K,V>> sequencedEntrySet ()

Returns a SequencedSet view of this map's entrySet.

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

final var entries = map.sequencedEntrySet();
System.out.println(entries); // [a=10, b=20, c=30]
System.out.println(entries.reversed()); // [c=30, b=20, a=10]

default SequencedSet<K> sequencedKeySet ()

Returns a SequencedSet view of this map's keySet.

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

final var keys = map.sequencedKeySet();
System.out.println(keys); // [a, b, c]
System.out.println(keys.reversed()); // [c, b, a]

default SequencedCollection<V> sequencedValues ()

Returns a SequencedCollection view of this map's values collection.

final SequencedMap<String, Integer> map = new LinkedHashMap<>();

map.putLast("a", 10);
map.putLast("b", 20);
map.putLast("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

final var values = map.sequencedValues();
System.out.println(values); // [10, 20, 30]
System.out.println(values.reversed()); // [30, 20, 10]

Methods declared in Map

clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values

Please see the link below.


Related posts

To top of page