Java : SequencedMap with Examples
SequencedMap (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SequencedMap methods.
Summary
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 ()
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 ()
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 ()
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 ()
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)
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)
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 ()
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 ()
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 ()
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 ()
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
- API Examples