NavigableSet in Java 6.0

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 NavigableTutorial.net;

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

public class NavigableSetTutorial1 {
    public static void main(String args[]){
        List list = new ArrayList();
        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));
       
    }
}


Output:
7
10

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.

No comments:

Post a Comment

Please Provide your feedback here