Last Updated on 16 June 2019   |   Print Email
In this tutorial, we are going to help you understand TreeMapas an implementation of NavigableMap interface. As you know in the SortedMap and TreeMap tutorial, besides Map and SortedMap, the TreeMap class also implements the NavigableMap interface.Now we go deeper to understand this interface in the Java Collections Framework.
1. Understanding NavigableMap
NavigableMap is sub interface of SortedMap interface, so it inherits all behaviors of a sorted map like range view, endpoints and comparator access operations. In addition, the NavigableMap interface provides navigation methods and descending views similar to NavigableSet. And due to the nature of a map which stores key-value mappings, the additional APIs are designed for both keys and key-value entries in the map.Let’s look at these methods in details.
Operations on key-value mappings (entries):
lowerEntry(K key): returns a key-value mapping associated with the greatest key strictly less than the given key.
floorEntry(K key): returns a key-value mapping entry which is associated with the greatest key less than or equal to the given key.
ceilingEntry(K key): returns an entry associated with the lest key greater than or equal to the given key.
higherEntry(K key): returns an entry associated with the least key strictly greater than the given key.
Note that all these methods return null if there is no such key.
firstEntry(): returns a key-value mapping associated with the least key in the map, or null if the map is empty.
lastEntry(): returns a key-value mapping associated with the greatest key in the map, or null if the map is empty.
descendingMap(): returns a reverse order view of the mappings contained in the map.
pollFirstEntry(): removes and returns a key-value mapping associated with the least key in the map, or null if the map is empty.
pollLastEntry(): removes and returns a key-value mapping associated with the greatest key in the map, or null if the map is empty.
Operations on keys only:
lowerKey(K key): returns the greatest key strictly less than the given key.
floorKey(K key): returns the greatest key less than or equal to the given key.
ceilingKey(K key): returns the least key greater than or equal to the given key.
higherKey(K key): returns the least key strictly greater than the given key.
descendingKeySet(): returns a NavigableSet containing the keys in reverse order.
Note that all these methods return null if there is no such key.
Furthermore, the NavigableMap interface overloads the headMap(), subMap() and tailMap() methods of the SortedMap interface, which accept additional arguments describing whether lower or upper bounds are inclusive versus exclusive:
headMap(K toKey, boolean inclusive)
subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
tailMap(K fromKey, boolean inclusive)
Now, let’s look at some code examples.
2. NavigableMap Examples with TreeMap
The following example shows us how to obtain the reverse order view of the keys in a map:
First entry: 100 => Continue
Last entry: 501 => Not Implemented
Lower entry: 400 => Bad Request
Floor entry: 401 => Unauthorized
Higher entry: 501 => Not Implemented
Ceiling entry: 500 => Internal Server Error
Map after first and last entries were polled:
200 => OK
400 => Bad Request
401 => Unauthorized
500 => Internal Server Error
That’s all about TreeMap as an implementation of the NavigableMap interface.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments