Home Tutorial Java Core Java Collection : NavigableSet Example

 
 

Java Collection : NavigableSet Example
Posted on: November 10, 2012 at 12:00 AM
Advertisement
In this tutorial we are describing NavigableSet of collection Framework

Java Collection : NavigableSet Example

In this tutorial we are describing NavigableSet of collection Framework

NavigableSet :

NavigableSet interface is declared in java.util package and extends SortedSet class. It provides methods for reporting closest matches for specified search targets. In NavigableSet, you can access elements either in ascending order or in descending order.
NavigableSet interface additionally provides methods pollFirst() and pollLast(). Here we are defining some of NavigableSet methods -

ceiling(E e) : This method returns the least element in NavigableSet greater than or equal to the specified element. If there is no such element exist then it will return null.

descendingIterator() : It returns an iterator over the elements in NavigableSet, in descending order.

pollFirst() : It retrieves and removes the first (lowest) element, or it returns null if your set is empty.

pollLast() : It retrieves and removes the last (highest) element, or returns null if your set is empty.

Other methods are - descendingSet(), floor(E e), headSet(E toElement), headSet(E toElement, boolean inclusive), higher(E e), iterator(), lower(E e),
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive), subSet(E fromElement, E toElement), tailSet(E fromElement),
tailSet(E fromElement, boolean inclusive).

Example : Here is an example of NavigableSet .

package collection;

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

class NavigableSetExample {
public static void main(String[] args){

//Creating object
NavigableSet<Integer> set=new TreeSet<Integer>();

//Inserting elements 
set.add(100);
set.add(400);
set.add(300);
set.add(222);
set.add(111);
System.out.println("NavigableSet after adding elements -");
System.out.println("Size : "+set.size());
System.out.println("Elements : "+set);

System.out.println("set.ceiling(100)- "+set.ceiling(100));
System.out.println("set.pollFirst()- "+set.pollFirst());

Iterator<Integer> it=set.descendingIterator();
System.out.println("Elements in descending order -");
while(it.hasNext()){
System.out.print(it.next()+", ");
}

//Removing elements
set.remove(111);
set.remove(300);
System.out.println("NavigableSet after deleting elements -");
System.out.println("Size : "+set.size());
System.out.println("Elements : "+set);
}
}

Output :

NavigableSet after adding elements -
Size : 5
Elements : [100, 111, 222, 300, 400]
set.ceiling(100)- 100
set.pollFirst()- 100
Elements in descending order -
400, 300, 222, 111, NavigableSet after deleting elements -
Size : 2
Elements : [222, 400]
Advertisement

Related Tags for Java Collection : NavigableSet Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 10, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com