Java Collections Tutorial Navigable Map Example

package JAVATUTORIAL;
import java.util.*;
import java.util.concurrent.*;

public class NavigableMapTutorial{
      public static void main(String[] args) {
        System.out.println("Navigable Map Example!\n");
        NavigableMap navMapObj = new
                           ConcurrentSkipListMap();
        navMapObj.put(1, "S");
        navMapObj.put(2, "February");
        navMapObj.put(3, "March");
        navMapObj.put(4, "April");
        navMapObj.put(5, "May");
        navMapObj.put(6, "June");
        navMapObj.put(7, "July");
        navMapObj.put(8, "August");
        navMapObj.put(9, "September");
        navMapObj.put(10, "October");
        navMapObj.put(11, "November");
        navMapObj.put(12, "December");
        //Displaying all data
        System.out.println("Data in Navigable Map is : " +
                                              navMapObj.descendingMap()+"\n");
        //Retrieving first data
        System.out.print("First data: " + navMapObj.firstEntry()+"\n");
        //Retrieving last data
        System.out.print("Last data: " + navMapObj.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: "
                                + navMapObj.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"+ navMapObj.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: " + navMapObj.higherEntry(5)+"\n");
        //Removing first
        System.out.println("Removing First: " + navMapObj.pollFirstEntry());
        //Removing last
        System.out.println("Removing Last: " + navMapObj.pollLastEntry()+"\n");
        //Displaying all data
        System.out.println("Now data: " + navMapObj.descendingMap());
      }
    }
     
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.

No comments:

Post a Comment

Please Provide your feedback here