Java : SortedMap with Examples

SortedMap (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SortedMap<K,V> methods.


Summary

A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods).

Class diagram

final SortedMap<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("ccc", 10);
sortedMap.put("ddd", 20);
sortedMap.put("aaa", 30);
sortedMap.put("bbb", 40);

System.out.println(sortedMap); // {aaa=30, bbb=40, ccc=10, ddd=20}

final var hashMap = new HashMap<String, Integer>();
hashMap.put("ccc", 10);
hashMap.put("ddd", 20);
hashMap.put("aaa", 30);
hashMap.put("bbb", 40);

System.out.println(hashMap); // {aaa=30, ccc=10, bbb=40, ddd=20}

Methods

Comparator<? super K> comparator ()

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

final var comparator = map.comparator();
System.out.println(comparator); // null
final SortedMap<String, Integer> map = new TreeMap<>(Comparator.reverseOrder());
System.out.println(map); // {}

final var comparator = map.comparator();
System.out.println(comparator.equals(Comparator.reverseOrder())); // true
final SortedMap<String, Integer> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
System.out.println(map); // {}

final var comparator = map.comparator();
System.out.println(comparator.equals(String.CASE_INSENSITIVE_ORDER)); // true

Set<Map.Entry<K,V>> entrySet ()

Returns a Set view of the mappings contained in this map.

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var entries = map.entrySet();
System.out.println(entries); // [a=1, b=2, c=3]

map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(entries); // [a=1, b=20, c=3]

entries.remove(Map.entry("a", 1));
System.out.println(map); // {b=20, c=3}
System.out.println(entries); // [b=20, c=3]

for (final var entry : entries) {
    entry.setValue(entry.getValue() * 10);
}

System.out.println(map); // {b=200, c=30}
System.out.println(entries); // [b=200, c=30]

K firstKey ()

Returns the first (lowest) key currently in this map.

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

map.put("b", 20);

System.out.println(map); // {b=20}
System.out.println(map.firstKey()); // b

map.put("c", 30);

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

map.put("a", 10);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.firstKey()); // a
final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

try {
    map.firstKey();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

SortedMap<K,V> headMap (K toKey)

Returns a view of the portion of this map whose keys are strictly less than toKey.

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);

System.out.println(map); // {x=10, y=20, z=30}

final var headMap = map.headMap("z");
System.out.println(headMap); // {x=10, y=20}

map.put("a", 40);

System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(headMap); // {a=40, x=10, y=20}

headMap.remove("x");

System.out.println(map); // {a=40, y=20, z=30}
System.out.println(headMap); // {a=40, y=20}
final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

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

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

Set<K> keySet ()

Returns a Set view of the keys contained in this map.

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

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

map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]

keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]

K lastKey ()

Returns the last (highest) key currently in this map.

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

map.put("b", 20);

System.out.println(map); // {b=20}
System.out.println(map.lastKey()); // b

map.put("a", 10);

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

map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.lastKey()); // c
final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

try {
    map.lastKey();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

default V putFirst (K k, V v)

Throws UnsupportedOperationException.

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // []

try {
    map.putFirst("a", 10);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

default V putLast (K k, V v)

Throws UnsupportedOperationException.

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // []

try {
    map.putLast("a", 10);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

default SortedMap<K,V> reversed ()

Returns a reverse-ordered view of this map.

final SortedMap<String, Integer> map = new TreeMap<>();

map.put("a", 10);
map.put("b", 20);
map.put("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}

SortedMap<K,V> subMap (K fromKey, K toKey)

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);

System.out.println(map); // {x=10, y=20, z=30}

final var subMap = map.subMap("a", "z");
System.out.println(subMap); // {x=10, y=20}

map.put("a", 40);

System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(subMap); // {a=40, x=10, y=20}

subMap.remove("x");

System.out.println(map); // {a=40, y=20, z=30}
System.out.println(subMap); // {a=40, y=20}
final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

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

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

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

SortedMap<K,V> tailMap (K fromKey)

Returns a view of the portion of this map whose keys are greater than or equal to fromKey.

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

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

final var tailMap = map.tailMap("b");
System.out.println(tailMap); // {b=20, c=30}

map.put("d", 40);

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

tailMap.remove("b");

System.out.println(map); // {a=10, c=30, d=40}
System.out.println(tailMap); // {c=30, d=40}
final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

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

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

Collection<V> values ()

Returns a Collection view of the values contained in this map.

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var values = map.values();
System.out.println(values); // [1, 2, 3]

map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(values); // [1, 20, 3]

values.remove(1);
System.out.println(map); // {b=20, c=3}
System.out.println(values); // [20, 3]

try {
    values.add(4);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

Methods declared in Map

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

Please see the link below.

Methods declared in SequencedMap

firstEntry, lastEntry, pollFirstEntry, pollLastEntry, sequencedEntrySet, sequencedKeySet, sequencedValues

Please see the link below.


Related posts

To top of page