E
higher(E e)
Returns
the least element in this set strictly greater than the given element
or null if there is no such element.
import java.util.*; class NavigableSetHigher{ public static void main(String args[]){ NavigableSet<Integer> mySet = new TreeSet<> (); /* Add Elements too mySet */ for(int i=20; i>0; i-=2){ mySet.add(i); } System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); for(int i=1; i<22; i+=2){ System.out.print("Least Higher Element of " + i +" is "); System.out.println(mySet.higher(i)); } } }
Output
Elements in mySet are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Least Higher Element of 1 is 2 Least Higher Element of 3 is 4 Least Higher Element of 5 is 6 Least Higher Element of 7 is 8 Least Higher Element of 9 is 10 Least Higher Element of 11 is 12 Least Higher Element of 13 is 14 Least Higher Element of 15 is 16 Least Higher Element of 17 is 18 Least Higher Element of 19 is 20 Least Higher Element of 21 is null
1.
throws NullPointerException if the specified element is null and this
set does not permit null elements
import java.util.*; class NavigableSetHigherNullPointer{ public static void main(String args[]){ NavigableSet<Integer> mySet = new TreeSet<> (); /* Add Elements too mySet */ for(int i=20; i>0; i-=2){ mySet.add(i); } System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); System.out.println("Least higher Element for null is"); mySet.higher(null); } }
Output
Elements in mySet are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Least higher Element for null is Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap.getHigherEntry(TreeMap.java:456) at java.util.TreeMap.higherKey(TreeMap.java:769) at java.util.TreeSet.higher(TreeSet.java:447) at NavigableSetHigherNullPointer.main(NavigableSetHigherNullPointer.java:16)
2.
throws ClassCastException if the specified element cannot be
compared
with the elements currently in the set
No comments:
Post a Comment