Thursday, August 18, 2022

What is NavigableMap in Java ? TreeMap, headMap, tailMap, and subMap Examples

Hello guys, if you are wondering what is NavigableMap and why should Java developer know about it you are at right place. NavigableMap in Java is an extension of SortedMap  like TreeMap which provides convenient navigation methods like lowerKey, floorKey, ceilingKey, and higherKey. NavigableMap is added on Java 1.6 and along with these popular navigation methods it also provides ways to create a Sub Map from existing Map in Java, like headMap whose keys are less than the specified key, tailMap whose keys are greater than specified key and a subMap which strictly contains keys that falls between toKey and fromKey

All of these methods also provide a boolean to include the specified key or not. TreeMap and ConcurrentSkipListMap are two concrete implementations of NavigableMap in Java 1.6 API.

Though NavigableMap is not as popular as HashMap, ConcurrentHashMap or Hashtable but given that TreeMap implements NavigableMap you already get all good things in a well-known Map implementation. 

And, I must tell you that TreeMap is an essential class for every Java developer. It's often used to keep object in sorted order for example, if you are creating an order cache then you can use TreeMap to keep the order sorted based upon time and price priority.  

It is also one of the most popular Map class for interview point of view. Almost all Java interviews ask about TreeMap and that why you will find it in every Java Collection interview question list. 

What is NavigableMap in Java ? TreeMap headMap, tailMap, and subMap Examples





How to use NavigableMap in Java? TreeMpa Example

In this Java tutorial, we will explore some API methods of NavigableMap to show its functionality. This Java program shows an example of lowerKey which returns keys less than specified, floorKey returns key less than or equal to, ceilingKey return greater than or equal to and higherKey which returns keys which are greater than the specified key. 

This Java example also demonstrates the use of headMap(), tailMap() and subMap() method which is used to create Map from an existing Map in Java. headMap returns a Map whose keys are lower than specified keys while tailMap returns Map which contains keys, that are higher than specified.  

You can also see these Java Collections and Stream courses to learn more about Collection Framework and Maps in Java

Here is a complete code example of How to use NavigableMap in Java.



import java.util.NavigableMap;
import java.util.TreeMap;

/**
 *
 * Java program to demonstrate What is NavigableMap in Java and How to use NavigableMap
 * in Java. NavigableMap provides two important features navigation methods
 * like lowerKey(), floorKey, ceilingKey() and higherKey().
 * There Entry counterpart and methods to create subMap e.g. headMap(), tailMap()
 * and subMap().
 *
 * @author Javin Paul
 */

public class NavigableMapExample {

    public static void main(String args[]) {
     
        //NavigableMap extends SortedMap to provide useful navigation methods
        NavigableMap<String, String> navigableMap = new TreeMap<String, String>();
     
        navigableMap.put("C++", "Good programming language");
        navigableMap.put("Java", "Another good programming language");
        navigableMap.put("Scala", "Another JVM language");
        navigableMap.put("Python", "Language which Google use");
     
        System.out.println("SorteMap : " + navigableMap);
     
        //lowerKey returns key which is less than specified key
        System.out.println("lowerKey from Java : "
                + navigableMap.lowerKey("Java"));
     
        // floorKey returns key which is less than
        // or equal to specified key

        System.out.println("floorKey from Java: "
          + navigableMap.floorKey("Java"));
     
        //ceilingKey returns key which is greater than
        // or equal to specified key

        System.out.println("ceilingKey from Java: "
           + navigableMap.ceilingKey("Java"));
     
        //higherKey returns key which is greater specified key
        System.out.println("higherKey from Java: "
           + navigableMap.higherKey("Java"));
     
     
        //Apart from navigation methods,
        // it also provides a useful method

        //to create subMap from existing Map 
        //  tailMap, headMap and subMap

     
        //an example of headMap - returns NavigableMap whose key is less than specified
        NavigableMap<String, String> headMap = navigableMap.headMap("Python", false);
        System.out.println("headMap created form navigableMap : " + headMap);
             
        //an example of tailMap - returns NavigableMap whose key is greater than specified
        NavigableMap<String, String> tailMap = navigableMap.tailMap("Scala", false);
        System.out.println("tailMap created form navigableMap : " + tailMap);
     
        //an example of subMap - return NavigableMap from toKey to fromKey
        NavigableMap<String, String> subMap
                  = navigableMap.subMap("C++", false 
                                         ,
"Python", false);
        System.out.println("subMap created form navigableMap : "
                                      + subMap);
    }
}

Output:
SorteMap : {C++=Good programming language, Java=Another good programming language, Python=Language which Google use, Scala=Another JVM language}
lowerKey from Java : C++
floorKey from Java: Java
ceilingKey from Java: Java
higherKey from Java: Python
headMap created form navigableMap : {C++=Good programming language, Java=Another good programming language}
tailMap created form navigableMap : {}
subMap created form navigableMap : {Java=Another good programming language}


How to create subMap from Map using Navigable Map in Java with ExampleThat's all on What is NavigableMap in Java and How to use NavigableMap for example. We have seen examples of popular navigation methods on TreeMap e.g. floorKey. You can also use similar methods like lowerEntry, floorEntry, ceilingEntry and higherEntry to retrieve Entry instead of a key. NavigableMap is also a good utility to create a subset of a Map in Java.


Other Java Collection tutorials from Javarevisited blog

No comments :

Post a Comment