NavigableSet in Java is an interface present in java.util package. It is part of the collection framework in java. NavigableSet is child interface of the SortedSet interface. NavigableSet came in Java 1.6 version. It behaves like a SortedSet exception that it defines several methods for navigation purposes. It can navigate the set in reverse order compared to the order defined in the SortedSet.
1. floor(E e): This method returns the highest element in this set which is less than or equal to the given element, it returns null if there is no such element.
Output:
Methods in Java NavigableSet.
1. floor(E e): This method returns the highest element in this set which is less than or equal to the given element, it returns null if there is no such element.
2. lower(E e): This method returns the highest element in this set which is less than to the given element, it returns null if there is no such element.
3. ceiling(E e): This method returns the least element in this set which is greater than or equal to the given element, it returns null if there is no such element.
4. higher(E e): This method returns the least element in this set which is greater than the given element, it returns null if there is no such element.
5. pollFirst(E e): This method is used to retrieve and remove the first least element, it returns null if there is no such element.
6. pollLast(E e): This method is used to retrieve and remove the last highest element, it returns null if there is no such element.
7. desecendingSet(): This method is used to returns the navigable set in reverse order.
7. desecendingSet(): This method is used to returns the navigable set in reverse order.
NavigableSet Example in Java:
import java.util.NavigableSet;
import java.util.TreeSet;
class Person
{
public static void main(String args[])
{
NavigableSet<Integer> nset = new TreeSet<Integer>();
nset.add(100);
nset.add(200);
nset.add(300);
nset.add(400);
nset.add(500);
nset.add(600);
nset.add(700);
//displaying normal order
System.out.println("Normal order of navigable set is: " +nset);
//displaying reverse order
System.out.println("Reverse order of navigable set is: " +nset.descendingSet());
System.out.println("Lower(300) is: " +nset.lower(300));
System.out.println("floor(400) is: " +nset.floor(400));
System.out.println("Ceiling(300) is: " +nset.ceiling(300));
System.out.println("Higher(200) is: " +nset.higher(200));
System.out.println("pool first is: " +nset.pollFirst());
System.out.println("pool last is: " +nset.pollLast());
}
}
Output:
The normal order of navigable set is: [100, 200, 300, 400, 500, 600, 700]
The reverse order of navigable set is: [700, 600, 500, 400, 300, 200, 100]
The reverse order of navigable set is: [700, 600, 500, 400, 300, 200, 100]
Lower(300) is: 200
floor(400) is: 400
Ceiling(300) is: 300
Higher(200) is: 300
poll first is: 100
poll last is: 700
floor(400) is: 400
Ceiling(300) is: 300
Higher(200) is: 300
poll first is: 100
poll last is: 700
You may enjoy these related posts:
NavigableSet in Java with Example
Reviewed by Prashant Srivastava
on
March 05, 2019
Rating:

No comments: