Java Reference
In-Depth Information
Iterator iter = ns.iterator();
while (iter.hasNext())
System.out.print(iter.next()+" ");
System.out.println();
System.out.print("descending order: ");
iter = ns.descendingIterator();
while (iter.hasNext())
System.out.print(iter.next()+" ");
System.out.println("\n");
outputClosestMatches(ns, 4);
outputClosestMatches(ns.descendingSet(), 12);
}
static void outputClosestMatches(NavigableSet<Integer>
ns, int i)
{
System.out.println("element
<
"+i+"
is
"+ns.lower(i));
System.out.println("element
<=
"+i+"
is
"+ns.floor(i));
System.out.println("element
>
"+i+"
is
"+ns.higher(i));
System.out.println("element
>=
"+i+"
is
"+ns.ceiling(i));
System.out.println();
}
}
Listing5-13 createsanavigablesetof Integer elements.Ittakesadvantageofauto-
boxing to ensure that int s are converted to Integers .
When you run this application, it generates the following output:
Ascending order: -13 -6 0 4 9 11 82
Descending order: 82 11 9 4 0 -6 -13
Element < 4 is 0
Element <= 4 is 4
Element > 4 is 9
Search WWH ::




Custom Search