Navigable Map is an extension of SortedMap that provide API's to conveniently navigate the TreeMap.
The below code is an example of the usage of Navigable Map.
The comments section makes it self explanatory.
NavigableMap<String,String> navigableMap=new TreeMap<String,String>();
navigableMap.put("J", "J");
navigableMap.put("A", "A");
navigableMap.put("I", "I");
navigableMap.put("Z", "Z");
navigableMap.put("B", "B");
// Returns the key that is lower than the specified key. Output : B
System.err.println("LOWERKEY "+navigableMap.lowerKey("I"));
// Returns the key that is less than or equal to the specified key. Output: B
System.err.println("FLOOR KEY "+navigableMap.floorKey("B"));
//Returns the key that is greater than or equal to the specified key. Output: I
System.err.println("CEILING KEY "+navigableMap.ceilingKey("I"));
//Returns the key that is greater than the key specified key. Output :J
System.err.println("HIGHER KEY "+navigableMap.higherKey("I"));
// Head Map Returns the list of keys that are lower than the specified key.
// The second argument is called inclusive. If specified as true, it returns
// the Map that are less than or equal to the specified key
NavigableMap<String,String> headMap=navigableMap.headMap("I",true);
System.err.println("Head Map "+headMap);
// Tail Map Returns the list of keys that are greater than the specified key.
// The second argument is called inclusive. If specified as true, it returns
// the Map that are greater than or equal to the specified key
NavigableMap<String,String> tailMap=navigableMap.tailMap("I",true);
System.err.println("Tail Map "+tailMap);
// Self Explanatory :-)
NavigableMap<String, String> subMap = navigableMap.subMap("I", false ,
"Z", true);
System.out.println("subMap : " + subMap);
If you have any other suggestions, Please leave a comment below.
The below code is an example of the usage of Navigable Map.
The comments section makes it self explanatory.
NavigableMap<String,String> navigableMap=new TreeMap<String,String>();
navigableMap.put("J", "J");
navigableMap.put("A", "A");
navigableMap.put("I", "I");
navigableMap.put("Z", "Z");
navigableMap.put("B", "B");
// Returns the key that is lower than the specified key. Output : B
System.err.println("LOWERKEY "+navigableMap.lowerKey("I"));
// Returns the key that is less than or equal to the specified key. Output: B
System.err.println("FLOOR KEY "+navigableMap.floorKey("B"));
//Returns the key that is greater than or equal to the specified key. Output: I
System.err.println("CEILING KEY "+navigableMap.ceilingKey("I"));
//Returns the key that is greater than the key specified key. Output :J
System.err.println("HIGHER KEY "+navigableMap.higherKey("I"));
// Head Map Returns the list of keys that are lower than the specified key.
// The second argument is called inclusive. If specified as true, it returns
// the Map that are less than or equal to the specified key
NavigableMap<String,String> headMap=navigableMap.headMap("I",true);
System.err.println("Head Map "+headMap);
// Tail Map Returns the list of keys that are greater than the specified key.
// The second argument is called inclusive. If specified as true, it returns
// the Map that are greater than or equal to the specified key
NavigableMap<String,String> tailMap=navigableMap.tailMap("I",true);
System.err.println("Tail Map "+tailMap);
// Self Explanatory :-)
NavigableMap<String, String> subMap = navigableMap.subMap("I", false ,
"Z", true);
System.out.println("subMap : " + subMap);
If you have any other suggestions, Please leave a comment below.