Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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>

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.