What is NavigableMap in Java 6 ?
NavigableMap in Java 6 is an extension of SortedMap like TreeMap which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey.it also provide ways to create a Sub Map from existing Map 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 specified toKey and fromKey.These methods also provides a boolean to include specified key or not.
NavigableMap in Java 6 is an extension of SortedMap like TreeMap which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey.it also provide ways to create a Sub Map from existing Map 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 specified toKey and fromKey.These methods also provides a boolean to include specified key or not.
import java.util.NavigableMap;
import java.util.TreeMap;
/**
*
* What is NavigableMap in Java.
* How to use NavigableMap in Java.
* NavigableMap provides important features - navigation methods
* like lowerKey(), floorKey, ceilingKey() and higherKey().
* There Entry counterpart and methods to create subMap e.g. headMap(), tailMap()
* and subMap().
*
*/
public class NavigableMapEx {
public static void main(String args[]) {
//NavigableMap extends SortedMap to provide useful navigation methods
NavigableMap navigableMap = new TreeMap();
navigableMap.put("BritneySpears", "I go through life like a karate kid");
navigableMap.put("AnnePackard", "Nothing happens without risk");
navigableMap.put("WillSmith", "Life is lived on the edge");
navigableMap.put("Cinderella", "Live like there is no midnight");
System.out.println("SorteMap:"+ navigableMap);
//lowerKey returns key which is less than specified key
System.out.println("lowerKey:"+ navigableMap.lowerKey("Cinderella"));
//floorKey returns key which is less than or equal to specified key
System.out.println("floorKey:"+ navigableMap.floorKey("AnnePackard"));
//ceilingKey returns key which is greater than or equal to specified key
System.out.println("ceilingKey:"+ navigableMap.ceilingKey("BritneySpears"));
//higherKey returns key which is greater specified key
System.out.println("higherKey:"+ navigableMap.higherKey("BritneySpears"));
//It also provides useful method to create subMap from existing Map
//e.g. tailMap, headMap and subMap
//headMap - returns NavigableMap whose key is less than specified
//param: Key, boolean : inclusive
NavigableMap headMap = navigableMap.headMap("Cinderella", false);
System.out.println("headMap created form navigableMap : " + headMap);
//tailMap - returns NavigableMap whose key is greater than specified
NavigableMap tailMap = navigableMap.tailMap("BritneySpears", false);
System.out.println("tailMap created form navigableMap : " + tailMap);
//subMap - return NavigableMap from toKey to fromKey
NavigableMap subMap =
navigableMap.subMap("AnnePackard", false,"WillSmith", false);
System.out.println("subMap created form navigableMap : " + subMap);
}
}
No comments:
Post a Comment