Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Navigable Map Example 
 

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

public class NavigableMapExample{
  public static void main(String[] args) {
    System.out.println("Navigable Map Example!\n");
    NavigableMap <Integer, String>navMap = new 
                      
ConcurrentSkipListMap<Integer, String>();
    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>

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.