Webcam Chat QuickBooks Advice international calling cards international phone cards
JavaBeat Java Books Certifications Certifications Kits Articles Tutorials Tips QNA Book Store Interview Questions SCJP 1.5 SCJP 1.6 SCWCD 5.0 SCBCD 5.0 SCEA SCJA Feeds
400 Mock Questions on SCJP 1.6 - JUST Rs.300 or 10 USD
Send us mail to sales@javabeat.net
more details
Do you have paypal account? Click Here to pay now and get the questions.

NavigableSet in Java 6.0

by JavaBeat(08/12/2007)

NavigableSet API is included in the SCJP 6.0 certification exam. This article explains few important methods with simple example program. NavigableSet is the subinterface of SortedSet. This interface defines methods for finding the element in a list. For example lower() method used for finding the element which is less than the given value. Look into the following example:


package javabeat.net;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetExample1 {
    public static void main(String args[]){
        List<Integer> list = new ArrayList<Integer>();
        list.add(3);
        list.add(4);
        list.add(1);
        list.add(8);
        list.add(7);
        list.add(10);
        
        NavigableSet navigableSet = new TreeSet(list);
        System.out.println(navigableSet.lower(8));
        System.out.println(navigableSet.higher(8));
        
    }
}

In the above code, NavigableSet.lower() method is used for reteriving the value which is less than '8' in the list. Same way NavigableSet.higher() is used for reteriving the value greater than '8' in the list. The output for the above program would be:

7
10
Other than finding less than or greater than values, NavigableSet provides few more useful methods to print the values in the desired order. Look into the following code snippet:

package javabeat.net;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetExample2 {
    public static void main(String args[]){
        List<Integer> list = new ArrayList<Integer>();
        list.add(4);
        list.add(6);
        list.add(9);
        list.add(5);
        NavigableSet navigableSet = new TreeSet(list);
        Iterator<Integer> iterator = (Iterator)navigableSet.descendingIterator();
        while (iterator.hasNext()){
            Integer value = iterator.next();
            System.out.println(value);
        }
        
    }
}
Above code demonstrates how to print the values in decending order using the NaviogableSet API
400 Mock Questions on SCJP 1.6 - JUST Rs.300 or 10 USD
Send us mail to sales@javabeat.net
more details

JavaBeat Website (2004-2011), India
javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2011), JavaBeat


Technology Blogs
Technology blogs Technology Blogs
blog log