Menu
Topics Index
...
`


Collections Framework > Map Interfaces >
Siva Nookala - 20 Feb 2017
The NavigableMap interface extends SortedMap and defines several methods for navigation purposes.

NOTE:
I -> Interface
C -> Class
The java.util package has only one implementation of the NavigableMap interface: java.util.TreeMap. There is an implementation in the java.util.concurrent package as well: java.util.concurrent.ConcurrentNavigableMap, which supports concurrent access.

NavigableMap is a generic interface with the following declaration: interface NavigableMap where K specifies the type of the keys and V specifies the type of the values associated with the keys.

Here we explore the following methods declared by NavigableMap.
Method Definition
ceilingEntry(K obj)Searches the map for the smallest key K such that k >= obj. If such a key is found, its entry is returned.Otherwise, null is returned.
lowerEntry(K obj)Searches the set for the largest key k such that k < obj. If such a key is found, its entry is returned. Otherwise, null is returned.
K ceilingKey( K obj ) Searches the map for the smallest key k such that k >= obj. If such a key is found, it is returned. Otherwise, null is returned.
K higherKey( K obj ) Searches the map for the smallest key k such that k > obj. If such a key is found, it is returned. Otherwise, null is returned.
K floorKey( K obj) Searches the map for the largest key k such that k <= obj. If such a key is found, it is returned. Otherwise, null is returned.
K lowerKey( K obj ) Searches the map for the largest key k such that k < obj. If sucha key is found, it is returned. Otherwise, null is returned.
firstEntry()Returns the first entry in the map. This is the entry with the least key.
floorEntry(K obj)Searches the map for the largest key K such that k <= obj. If such a key is found, its entry is returned. Otherwise, null is returned.
Map.Entry<K, V>pollFirstEntry() Returns and removes the first entry (key + value) in the NavigableMap or null if the map is empty.
Map.Entry<K, V>pollLastEntry() Returns and removes the last entry (key + value) in the NavigableMap or null if the map is empty.
NavigableMap<K, V>descendingMap() Returns a NavigableMap which is a view of the original Map. The order of the elements in this view map is reverse of the order of the original map.
subMap(K lowerBound, boolean lowIncl, K upperBound, boolean highIncl)Returns a Navigable Map that includes all entries from the invoking map that have keys that are greater than lowerBound and less than upperBound. If lowerIncl is true, then an element equal to lowerBound is included. If highIncl is true, then an element equal to highIncl is included. The resulting map is backed by the invoking map.
tailMap(K lowerBound, boolean incl)Returns a Navigable Map that includes all entries from the invoking map that have keys that are greater than lowerBound.If incl is true, then an element equal to lowerBound is included. If highIncl is true, then an element equal to highIncl is included. The resulting map is backed by the invoking map.
descendingKeySet()Returns a NavigableSet that contains the keys in the invoking map in reverse order. Thus, it returns a reverse set-view of the keys. The resulting set is backed by the map.
navigableKeySet()Returns a NavigableSet that contains the keys in the invoking map. The resulting set is backed by the invoking map.
higherEntry(K obj) Searches the set for the largest key K such that k > obj. If such a key is found, its entry is returned. Otherwise, null is returned.
lastEntry()Returns the last entry in the map. This is the entry with the largest key.
The following program demonstrates these methods:
NavigableMapDemo
import java.util.*;

class NavigableMapDemo
{
    public static void main(String arg[])
    {
        TreeMap<String, String> tm = new TreeMap<String, String>();
        tm.put("S", "Sachin");
        tm.put("D", "Dhoni");
        tm.put("Y", "Yuvi");
        tm.put("G", "Ganguly");
        tm.put("K", "Kohli");
        System.out.println(tm);
        System.out.println(tm.ceilingKey("D"));
        System.out.println(tm.higherKey("E"));
        System.out.println(tm.floorKey("E"));
        System.out.println(tm.lowerKey("E"));
        System.out.println(tm.pollFirstEntry());
        System.out.println(tm.pollLastEntry());
        System.out.println(tm.descendingMap());
        System.out.println(tm);    
    }
}
OUTPUT

{D=Dhoni, G=Ganguly, K=Kohli, S=Sachin, Y=Yuvi}
D
G
D
D
D=Dhoni
Y=Yuvi
{S=Sachin, K=Kohli, G=Ganguly}
{G=Ganguly, K=Kohli, S=Sachin}

DESCRIPTION

NOTE: Observe the method combinations: ceilingKey() - higherKey() and floorKey() - lowerKey()
1) The ceilingKey() method returns the least or smallest key in the map that is greater than or equal to the element passed in as parameter.

2) The floorKey() method returns the greatest key in the map that is less than or equal to the element passed in as parameter.

3) The higherKey() method returns the least or smallest element in the map that is greater than (but not equal to) the element passed in as parameter.

4) The lowerKey() method returns the greatest key in the map that is less than (but not equal to) the element passed in as parameter.

Let’s take a look at another example that explains how these methods work:
NavigableMapDemoUsingMethods
import java.util.*;

class NavigableMapDemoUsingMethods
{
    public static void main(String arg[])
    {
        NavigableMap nm = new TreeMap();
        nm.put("1", "1");
        nm.put("2", "2");
        nm.put("3", "3");
        System.out.println(nm.ceilingKey("2")); // ceilingKey is 2
        System.out.println(nm.floorKey("2")); // floorKey is 2
        System.out.println(nm.higherKey("2")); // higherKey is 3
        System.out.println(nm.lowerKey("2")); // lowerKey is 1    
    }
}
OUTPUT

2
2
3
1

DESCRIPTION

THINGS TO TRY
  • Consider the above program. Try adding few more entries to map and then call celingKey(), floorKey(), higherKey() and lowerKey() methods to get a more clear picture of these methods.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.