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 Set Example

                         

In the example below we have used NavigableSet method to sort the elements in ascending order, descending order, also to retrieve the element which is immediately greater than or equal to 35 etc. With the help of NavigableSet methods its just a method call to get the result. It is used to return the closest matches of elements for the given elements in the collection.  

Description of program:

Inserting the data in the NavigableSet by the add() method. The NavigableSet provides the facility to getting the data in both orders: ascending and descending orders. The descendingSet() method returns the data from the NavigableSet in descending order. If you want to get the data in ascending order must be used an iterator. An iterator stores the data as a index and the hasNext() method returns 'true' until, the next() method returns the element in ascending order. 

Here, you remove the element from the NavigableSet at first and last position, you use the pollFirst() and pollLast() methods. Sometimes, you want to get the less and greater than or equal to the given element by using the floor() and ceiling() methods. For getting the all elements of the NavigableSet that is greater than or equal to the given element by the tailSet() method and less than or equal to the given element of the NavigableSet by the headSet() method. 

Here is the code of program:

import java.util.*;
import java.util.concurrent.*;

public class NavigableSetExample{
  public static void main(String[] args) {
    System.out.println("Navigable set Example!\n");
    NavigableSet <Integer>nSet = new ConcurrentSkipListSet<Integer>();
    nSet.add(10);
    nSet.add(20);
    nSet.add(50);
    nSet.add(30);
    nSet.add(100);
    nSet.add(80);
    // Returns an iterator over the elements in navigable set,
                    in ascending order.

    Iterator iterator = nSet.iterator();
    System.out.print("Ascending order navigable set: ");
    //Ascending order list
    while (iterator.hasNext()){
      System.out.print(iterator.next() " ");
    }
    System.out.println();
    //Descending order list
    System.out.println("Descending order navigable set: " 
                             nSet.descendingSet
() "\n");
    //Greater than or equal to the given element
    System.out.println("Least element in Navigable set greater than 
                         or equal to 35: " 
+ nSet.ceiling(35));
    //Less than or equal to the given element
    System.out.println("Greatest element in Navigable set less than 
                         or equal to 35: " 
+ nSet.floor(35"\n");
    //Viewing the portion of navigable set whose elements are 
                          strictly less than the given element

    System.out.println("Navigable set whose elements are strictly 
                         less than '40': " 
+ nSet.headSet(40));
    //Viewing the portion of navigable set whose elements are 
                  greater than or equal to the given element

    System.out.println("Navigable set whose elements are greater 
                than or equal to '40': " 
+ nSet.tailSet(40"\n");
    //Removing first element from navigable set
    System.out.println("Remove element: "+nSet.pollFirst());
    //After removing the first element, now get navigable set
    System.out.println("Now navigable set: " + nSet.descendingSet() "\n");
    //Removing last element from navigable set
    System.out.println("Remove element: " + nSet.pollLast());
    //After removing the last element, now get navigable set
    System.out.println("Now navigable set: " + nSet.descendingSet());
  }
}

 Download this Example.

Output of this program

C:\vinod\collection>javac NavigableSetExample.java

C:\vinod\collection>java NavigableSetExample
Navigable set Example!

Ascending order navigable set: 10 20 30 50 80 100
Descending order navigable set: [100, 80, 50, 30, 20, 10]

Least element in Navigable set greater than or equal to 35: 50
Greatest element in Navigable set less than or equal to 35: 30

Navigable set whose elements are strictly less than '40': [10, 20, 30]
Navigable set whose elements are greater than or equal to '40': [50, 80, 100]

Remove element: 10
Now navigable set: [100, 80, 50, 30, 20]

Remove element: 100
Now navigable set: [80, 50, 30, 20]

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.