Java : NavigableMap with Examples

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


Summary

A SortedMap extended with navigation methods returning the closest matches for given search targets. Methods lowerEntry(K), floorEntry(K), ceilingEntry(K), and higherEntry(K) return Map.Entry objects associated with keys respectively less than, less than or equal, greater than or equal, and greater than a given key, returning null if there is no such key.

Class diagram

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

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.size()); // 3

System.out.println(map.lowerEntry("a")); // null
System.out.println(map.lowerEntry("b")); // a=10
System.out.println(map.lowerEntry("c")); // b=20

System.out.println(map.higherEntry("a")); // b=20
System.out.println(map.higherEntry("b")); // c=30
System.out.println(map.higherEntry("c")); // null

Methods

Map.Entry<K,V> ceilingEntry (K key)

Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.ceilingEntry("a")); // b=10
System.out.println(map.ceilingEntry("b")); // b=10
System.out.println(map.ceilingEntry("c")); // d=20
System.out.println(map.ceilingEntry("d")); // d=20
System.out.println(map.ceilingEntry("e")); // f=30
System.out.println(map.ceilingEntry("f")); // f=30
System.out.println(map.ceilingEntry("g")); // null

K ceilingKey (K key)

Returns the least key greater than or equal to the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.ceilingKey("a")); // b
System.out.println(map.ceilingKey("b")); // b
System.out.println(map.ceilingKey("c")); // d
System.out.println(map.ceilingKey("d")); // d
System.out.println(map.ceilingKey("e")); // f
System.out.println(map.ceilingKey("f")); // f
System.out.println(map.ceilingKey("g")); // null

NavigableSet<K> descendingKeySet ()

Returns a reverse order NavigableSet view of the keys contained in this map.

final NavigableMap<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 descKeys = map.descendingKeySet();
System.out.println(descKeys); // [c, b, a]

map.put("d", 40);

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

descKeys.remove("b");

System.out.println(map); // {a=10, c=30, d=40}
System.out.println(descKeys); // [d, c, a]

NavigableMap<K,V> descendingMap ()

Returns a reverse order view of the mappings contained in this map.

final NavigableMap<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 descMap = map.descendingMap();
System.out.println(descMap); // {c=30, b=20, a=10}

map.put("d", 40);

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

descMap.remove("b");

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

Map.Entry<K,V> firstEntry ()

Returns a key-value mapping associated with the least key in this map, or null if the map is empty.

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

map.put("b", 20);

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

map.put("c", 30);

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

map.put("a", 10);

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

Map.Entry<K,V> floorEntry (K key)

Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.floorEntry("a")); // null
System.out.println(map.floorEntry("b")); // b=10
System.out.println(map.floorEntry("c")); // b=10
System.out.println(map.floorEntry("d")); // d=20
System.out.println(map.floorEntry("e")); // d=20
System.out.println(map.floorEntry("f")); // f=30
System.out.println(map.floorEntry("g")); // f=30

K floorKey (K key)

Returns the greatest key less than or equal to the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.floorKey("a")); // null
System.out.println(map.floorKey("b")); // b
System.out.println(map.floorKey("c")); // b
System.out.println(map.floorKey("d")); // d
System.out.println(map.floorKey("e")); // d
System.out.println(map.floorKey("f")); // f
System.out.println(map.floorKey("g")); // f

SortedMap<K,V> headMap (K toKey)

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

final NavigableMap<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 NavigableMap<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}

NavigableMap<K,V> headMap (K toKey, boolean inclusive)

Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.

final NavigableMap<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("y", true);
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 NavigableMap<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("a", true)); // {a=10}

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

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

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

Map.Entry<K,V> higherEntry (K key)

Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.higherEntry("a")); // b=10
System.out.println(map.higherEntry("b")); // d=20
System.out.println(map.higherEntry("c")); // d=20
System.out.println(map.higherEntry("d")); // f=30
System.out.println(map.higherEntry("e")); // f=30
System.out.println(map.higherEntry("f")); // null
System.out.println(map.higherEntry("g")); // null

K higherKey (K key)

Returns the least key strictly greater than the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.higherKey("a")); // b
System.out.println(map.higherKey("b")); // d
System.out.println(map.higherKey("c")); // d
System.out.println(map.higherKey("d")); // f
System.out.println(map.higherKey("e")); // f
System.out.println(map.higherKey("f")); // null
System.out.println(map.higherKey("g")); // null

Map.Entry<K,V> lastEntry ()

Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

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

map.put("b", 20);

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

map.put("a", 10);

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

map.put("c", 30);

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

Map.Entry<K,V> lowerEntry (K key)

Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.lowerEntry("a")); // null
System.out.println(map.lowerEntry("b")); // null
System.out.println(map.lowerEntry("c")); // b=10
System.out.println(map.lowerEntry("d")); // b=10
System.out.println(map.lowerEntry("e")); // d=20
System.out.println(map.lowerEntry("f")); // d=20
System.out.println(map.lowerEntry("g")); // f=30

K lowerKey (K key)

Returns the greatest key strictly less than the given key, or null if there is no such key.

final NavigableMap<String, Integer> map = new TreeMap<>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.lowerKey("a")); // null
System.out.println(map.lowerKey("b")); // null
System.out.println(map.lowerKey("c")); // b
System.out.println(map.lowerKey("d")); // b
System.out.println(map.lowerKey("e")); // d
System.out.println(map.lowerKey("f")); // d
System.out.println(map.lowerKey("g")); // f

Map.Entry<K,V> pollFirstEntry ()

Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

final NavigableMap<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.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

Map.Entry<K,V> pollLastEntry ()

Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

final NavigableMap<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.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 NavigableMap<K,V> reversed ()

Returns a reverse-ordered view of this map.

final NavigableMap<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}

NavigableMap<K,V> subMap (K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

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

final NavigableMap<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", true, "y", true);
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 NavigableMap<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", true, "c", true)); // {a=10, b=20, c=30}
System.out.println(map.subMap("a", true, "c", false)); // {a=10, b=20}
System.out.println(map.subMap("a", false, "c", true)); // {b=20, c=30}
System.out.println(map.subMap("a", false, "c", false)); // {b=20}

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

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

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 NavigableMap<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 NavigableMap<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 NavigableMap<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 NavigableMap<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")); // {}

NavigableMap<K,V> tailMap (K fromKey, boolean inclusive)

Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.

final NavigableMap<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("a", false);
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 NavigableMap<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("a", false)); // {b=20, c=30}

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

System.out.println(map.tailMap("c")); // {c=30}
System.out.println(map.tailMap("c", false)); // {}

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

sequencedEntrySet, sequencedKeySet, sequencedValues

Please see the link below.

Methods declared in SortedMap

comparator, entrySet, firstKey, keySet, lastKey, putFirst, putLast, values

Please see the link below.


Related posts

To top of page