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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest 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.

 

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>

                         

» 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.