In NavigableMap we use methods to return the key value pair like navMap.put(1, "January"); whereas in NavigableSet we use methods to return values. ConcurrentSkipListMap is the one of the class which implements NavigableMap.
Navigable Map Example
We already know that NavigableMap is similar to NavigableSet. In NavigableMap we use methods to return the key value pair like navMap.put(1, "January"); whereas in NavigableSet we use methods to return values. ConcurrentSkipListMap is the one of the class which implements NavigableMap. Lets have a look at the example.Description of program:
The following program helps you in inserting, removing and retrieving the data from the NavigableMap. It uses the put() method to add the element. If you want to retrieve the data at first and last position from the NavigableMap, you use the firstEntry() and lastEntry() methods. The descendingMap() method represents all data to the NavigableMap in descending order.
You can retrieve the nearest less than or equal to the given number and the greatest key strictly less than the given number floorEntry() and lowerEntry() methods. And you retrieve a key-value associated with the least key strictly greater than the given key, you use the higherEntry() method. The pollFirstEntry() method removes the first data from the NavigableMap and pollLastEntry() method also removes the data at the last position from the NavigableMap.
Here is the code of program:
import java.util.*;
|
Output of program:
C:\vinod\collection>javac NavigableMapExample.java C:\vinod\collection>java NavigableMapExample Navigable Map Example! Data in the navigable map: {12=December, 11=November, 10=October, 9=September, 8 =August, 7=July, 6=June, 5=May, 4=April, 3=March, 2=February, 1=January} First data: 1=January Last data: 12=December Nearest less than or equal to the given key: 5=May Retrieving the greatest key strictly less than the given key: 2=February Retriving data from navigable map greter than the given key: 6=June Removing First: 1=January Removing Last: 12=December Now data: {11=November, 10=October, 9=September, 8=August, 7=July, 6=June, 5=May , 4=April, 3=March, 2=February} C:\vinod\collection> |