|
|
|
navMap.put(12, “December”); //Displaying all data
System.out.println(“Data in the navigable map: “ +
navMap.descendingMap()+”\n”); //Retrieving first data
System.out.print(“First data: “ + navMap.firstEntry()+”\n”);
//Retrieving last data
System.out.print(“Last data: “ + navMap.lastEntry()+”\n\n”);
//Retrieving the nearest less than or equal to the given key
System.out.print(“Nearest less than or equal to the given key:
“ + navMap.floorEntry(5)+”\n”);
//Retrieving the greatest key strictly less than the given key
System.out.println(“Retrieving the greatest key strictly less
than the given key: “ + navMap.lowerEntry(3));
//Retrieving a key-value associated with the least key strictly
greater than the given key
System.out.println(“Retriving data from navigable map greater
than the given key: “ + navMap.higherEntry(5)+”\n”);
//Removing first
System.out.println(“Removing First: “ +
navMap.pollFirstEntry()); //Removing last
System.out.println(“Removing Last: “ +
navMap.pollLastEntry()+”\n”); //Displaying all data
System.out.println(“Now data: “ + navMap.descendingMap());
}}
|
Output:
C:\Javabasics\collection>javac NavigableMapExample.java
C:\Javabasics\collection>java NavigableMapExample
Navigable Map Example!Data in the navigable map:
{
12=December, 11=November, 10=October, 9=September,
8=August, 7=July, 6=June, 5=May, 4=April, 3=March, 2=February,
1=January
}
First data: 1=January
Last data: 12=December
Nearest less than or equal to the given key: 5=May
Retrieving the greatest key strictly less than the given key:
2=February
Retriving data from navigable map greater than the given key:
6=June
Removing First: 1=January
Removing Last: 12=December
Now data: {11=November, 10=October, 9=September, 8=August,
7=July, 6=June, 5=May, 4=April, 3=March, 2=February}
C:\Javabasics\collection> |
|
|
Now, let’s see the features of the NaviagableSet using an example.
In the example below the NavigableSet method is used
to sort the elements in ascending order, descending
order and to retrieve the element which is immediately
greater than or equal to 35 etc by returning the closest
matches of elements for the given elements in the collection.
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: “ + |
|
|
July 2007 | Java Jazz Up |14 |
|
|
|
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , Download PDF |
|
|
|
|
|
|
|
|