Sorted Maps and Navigable Maps – Collections: Part II
By Jaime Williams / May 23, 2023 / No Comments / Composing Stream Pipelines, Oracle Certification Exam, The NavigableSet Interface
15.10 Sorted Maps and Navigable Maps
The SortedMap<K, V> interface extends the Map<K, V> interface, and the NavigableMap<K, V> interface extends the SortedMap<K, V> interface. The two maps are analogs of the SortedSet<E> and the NavigableSet<E> interfaces, respectively.
The SortedMap<K, V> Interface
The SortedMap<K, V> interface extends the Map<K, V> interface to provide the functionality for implementing maps with sorted keys. Its operations are analogous to those of the SortedSet<E> interface (p. 810), applied to maps and keys rather than to sets and elements.
// First-last keys
K firstKey()
Sorted set:
first()
K lastKey()
Sorted set:
last()
Return the first (smallest) key and the last (largest) key in the sorted map, respectively, dependent on the key-sort order in the map. They throw a NoSuchElementException if the map is empty.
// Range-view operations
SortedMap<K,V> headMap(K toKey)
Sorted set:
headSet()
SortedMap<K,V> tailMap(K fromKey)
Sorted set:
tailSet()
SortedMap<K,V> subMap(K fromKey, K toKey)
Sorted set:
subSet()
Return different views analogous to those for a SortedSet<E>. The views returned are a portion of the map whose keys are strictly less than toKey, greater than or equal to fromKey, and between fromKey (inclusive) and toKey (exclusive), respectively. That is, these partial map views include fromKey if it is present in the map, but toKey is excluded.
// Comparator access
Comparator<? super K> comparator()
Returns the key comparator that defines the key-sort order, or null if the sorted map uses natural ordering for the keys.
The NavigableMap<K, V> Interface
Analogous to the NavigableSet<E> interface extending the SortedSet<E> interface, the NavigableMap<K, V> interface extends the SortedMap<K, V> interface with navigation methods to find the closest matches for specific search targets. The NavigableMap<K, V> interface replaces the SortedMap<K, V> interface and is the preferred choice when a sorted map is needed.
In addition to the methods of the SortedMap<K, V> interface, the NavigableMap<K, V> interface adds the new methods shown below, where the analogous methods from the NavigableSet<E> interface are also identified. Note that where a NavigableMap<K, V> method returns a Map.Entry object representing an entry, the corresponding NavigableSet<E> method returns an element of the set.
// First-last entries
// Remove
Map.Entry<K, V> pollFirstEntry()
Navigable set:
pollFirst()
Map.Entry<K, V> pollLastEntry()
Navigable set:
pollLast()
// Examine
Map.Entry<K, V> firstEntry()
Map.Entry<K, V> lastEntry()
The pollFirstEntry() method removes and returns the first entry, and the poll-LastEntry() method removes and returns the last entry currently in this navigable map. The entry is determined according to the ordering policy employed by the map—for example, natural ordering. Both return null if the navigable set is empty. The last two methods only retrieve, and do not remove, the value that is returned.
// Range-view operations
NavigableMap<K, V> headMap(K toKey,
Navigable set:
headSet()
boolean inclusive)
NavigableMap<K, V> tailMap(K fromKey,
Navigable set:
tailSet()
boolean inclusive)
NavigableMap<K, V> subMap(K fromKey,
Navigable set:
subSet()
boolean fromInclusive,
K toKey,
boolean toInclusive)
These operations are analogous to the ones in the SortedMap<K, V> interface (p. 845), returning different views of the underlying navigable map, depending on the bound elements. However, the bound elements can be excluded or included by the operation, depending on the value of the boolean argument inclusive.
// Closest-matches
Map.Entry<K, V> ceilingEntry(K key)
Navigable set:
ceiling()
K ceilingKey(K key)
Map.Entry<K, V> floorEntry(K key)
Navigable set:
floor()
K floorKey(K key)
Map.Entry<K, V> higherEntry(K key)
Navigable set:
higher()
K higherKey(K key)
Map.Entry<K, V> lowerEntry(K key)
Navigable set:
lower()
K lowerKey(K key)
The ceiling methods return the least entry (or key) in the navigable map >= to the argument key. The floor methods return the greatest entry (or key) in the navigable map <= to the argument key. The higher methods return the least entry (or key) in the navigable map > the argument key. The lower methods return the greatest entry (or key) in the navigable map < the argument key. All methods return null if there is no such key.
// Navigation-views
NavigableMap<K, V> descendingMap()
Navigable set:
descendingSet()
NavigableSet<K> descendingKeySet()
NavigableSet<K> navigableKeySet()
The first method returns a reverse-order map view of the entries in the navigable map. The second method returns a reverse-order key set view for the entries in the navigable map. The last method returns an ascending-order key set view for the entries in the navigable map.