Search This Blog

Saturday, February 4, 2012

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.*;
import java.util.concurrent.*;

public class NavigableMapExample{
public static void main(String[] args) {
System.out.println("Navigable Map Example!\n");
NavigableMap navMap = new
ConcurrentSkipListMap();
navMap.put(1, "January");
navMap.put(2, "February");
navMap.put(3, "March");
navMap.put(4, "April");
navMap.put(5, "May");
navMap.put(6, "June");
navMap.put(7, "July");
navMap.put(8, "August");
navMap.put(9, "September");
navMap.put(10, "October");
navMap.put(11, "November");
navMap.put(12, "December");
//Displaying all data
System.out.println("Data in the navigable map: " +
navMap.descendingMap
()+"\n");
//Retrieving first data
System.out.print("First data: " + navMap.firstEntry()+"\n");
//Retrieving last data
System.out.print("Last data: " + navMap.lastEntry()+"\n\n");
//Retrieving the nreatest less than or equal to the given key
System.out.print("Nearest less than or equal to the given key: "
+ navMap.floorEntry(5)+"\n");
//Retrieving the greatest key strictly less than the given key
System.out.println("Retrieving the greatest key strictly less than
the given key: "
+ navMap.lowerEntry(3));
//Retrieving a key-value associated with the least key
strictly greater than the given key

System.out.println("Retriving data from navigable map greter than
the given key: "
+ navMap.higherEntry(5)+"\n");
//Removing first
System.out.println("Removing First: " + navMap.pollFirstEntry());
//Removing last
System.out.println("Removing Last: " + navMap.pollLastEntry()+"\n");
//Displaying all data
System.out.println("Now data: " + navMap.descendingMap());
}
}

Download this example.

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>

No comments:

Post a Comment