dsa

Java NavigableMap Interface

In this tutorial, we will learn about the Java NavigableMap interface and its methods with the help of a example.

The NavigableMap interface of the Java collections framework provides the features to navigate among the map entries.

NavigableMap is an extension of SortedMap which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey, and along with these popular navigation method it also provide ways to create a Sub Map from existing Map in Java e.g. headMap whose keys are less than specified key, tailMap whose keys are greater than specified key and a subMap which is strictly contains keys which falls between toKey and fromKey.

navigablemap

Example:

// Java program to demonstrate NavigableMap 
import java.util.NavigableMap; 
import java.util.TreeMap; 

public class Example 
{ 
	public static void main(String[] args) 
	{ 
		NavigableMap<String, Integer> nm = 
						new TreeMap<String, Integer>(); 
		nm.put("C", 888); 
		nm.put("Y", 999); 
		nm.put("A", 444); 
		nm.put("T", 555); 
		nm.put("B", 666); 
		nm.put("A", 555); 

		System.out.printf("Descending Set : %s%n", 
								nm.descendingKeySet()); 
		System.out.printf("Floor Entry : %s%n", 
								nm.floorEntry("L")); 
		System.out.printf("First Entry : %s%n", 
								nm.firstEntry()); 
		System.out.printf("Last Key : %s%n", 
								nm.lastKey()); 
		System.out.printf("First Key : %s%n", 
								nm.firstKey()); 
		System.out.printf("Original Map : %s%n", nm); 
		System.out.printf("Reverse Map : %s%n", 
								nm.descendingMap()); 
	} 
} 

output:
Descending Set : [Y, T, C, B, A]
Floor Entry : C=888
First Entry : A=555
Last Key : Y
First Key : A
Original Map : {A=555, B=666, C=888, T=555, Y=999}
Reverse Map : {Y=999, T=555, C=888, B=666, A=555}

Methods of NavigableMap

Methods Description
ceilingEntry(K key) Returns a key-value mapping which is associated with the least key which is greater than or equal to the specified key. Also, returns null if there is no such key.
ceilingKey(K key) Returns the least key which is greater than or equal to the specified key. Also, returns null if there is no such key.
descendingKeySet() Returns the reverse order NavigableSet view for the given keys which are contained in the map.
descendingMap() Returns the reverse order view of the mapping present in the map.
firstEntry() Returns a key-value mapping which is associated with the least key in the given map. Also, returns null if the map is empty.
floorEntry(K key) Returns a key-value mapping which is associated with the greatest key which is less than or equal to the given key. Also, returns null if the map is empty.
floorKey(K key) Returns the greatest key which is less than or equal to the given key. Also, returns null if the map is empty.
headMap(K toKey) Returns a view of the map whose keys are strictly less than the toKey.
headMap(K toKey, Boolean inclusive) Returns a view of the map whose keys are less than(if inclusive is true) the toKey.
higherEntry(K key) Returns a key-value mapping associated with the least key which is strictly greater than the given key. Also, returns null if there is no such key.
higherKey(K key) Returns the least key which is strictly greater than the given key. Also, returns null if there is no such key.
lastEntry() Returns a key-value mapping which is associated with the greatest key in the given map. Also, returns null if the map is empty.
lowerEntry(K key) Returns a key-value mapping which is associated with the greatest key less than the specified key. Also, returns null if the map is empty.
lowerKey(K key) Returns the greatest key such that it is strictly less than the given key. Also, it may return null if there is no key.
navigableKeySet() Returns a NavigableSet view of the keys which are contained in the map.
pollFirstEntry() Removes and returns a key-value mapping which is associated with the least key present in the map. Also, it may return null if the map is empty.
apollLastEntry() Removes and returns a key-value mapping which is associated with the greatest key present in the map. Also, it may return null if the map is empty.
subMap(K fromKey, Boolean fromInclusive, K toKey, Boolean toInclusive) Returns a view of the portion of the map whose keys may range from fromKey to toKey.
subMap(K fromKey, K toKey) Returns a view of the portion of the map whose keys may range from fromKey, inclusive to toKey, exclusive.
tailMap(K fromKey) Returns a view of the portion of the map whose keys are greater thantailMap(K fromKey, boolean inclusive)
tailMap(K fromKey, boolean inclusive) Returns a view of the portion of the map whose keys are greater than (or equal to) fromKey.