Tuesday, May 24, 2011

ConcurrentSkipListMap Class in java Collections

ConcurrentSkipListMap is one of the class which implements NavigableMap.
This class implement all of the optional methods of the Map and Iterator interfaces. Like most other concurrent collections, this class does not permit the use of null keys or values because some null return values cannot be reliably distinguished from the absence of elements. NavigableMap class is similar to NavigableSet. In NavigableMap we use methods to return the key value pair like navMap.put(1, "sunday") whereas in NavigableSet we use methods to return values.


NavigableMap navmap=new ConcurrentSkipListMap();
navmap.put(1, "Sunday");

navmap.put(2, "Monday");

navmap.put(3, "Tuesday");

navmap.put(4, "Wednesday");

navmap.put(5, "Thursday");

navmap.put(6, "Friday");

navmap.put(7, "Saturday");

//Retrieving first data
String first = navmap.firstEntry();
//Retrieving last data
String last = navmap.lastEntry();
//Retrieving the nearest less than or equal to the given key
String closeFloorKeyVal = navmap.floorEntry(5);
//Retrieving the greatest key strictly less than the given key
String less = navmap.lowerEntry(3);
//Retrieving a key-value associated with the least key
//strictly greater than the given key
String higher=navmap.higherEntry(5);
//Removing first
String remFirst = navmap.pollFirstEntry();
//Removing last
String remLast = navmap.pollLastEntry();

No comments:

Post a Comment