SortedSet<E>
headSet(E toElement)
Returns
a view of the portion of this set whose elements are strictly less
than toElement.
import java.util.*; class NavigableSetHeadSet{ 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("HeadSet for value " + i +" is "); System.out.println(mySet.headSet(i)); } } }
Output
Elements in mySet are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] HeadSet for value 1 is [] HeadSet for value 3 is [2] HeadSet for value 5 is [2, 4] HeadSet for value 7 is [2, 4, 6] HeadSet for value 9 is [2, 4, 6, 8] HeadSet for value 11 is [2, 4, 6, 8, 10] HeadSet for value 13 is [2, 4, 6, 8, 10, 12] HeadSet for value 15 is [2, 4, 6, 8, 10, 12, 14] HeadSet for value 17 is [2, 4, 6, 8, 10, 12, 14, 16] HeadSet for value 19 is [2, 4, 6, 8, 10, 12, 14, 16, 18] HeadSet for value 21 is [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
1.
The returned set is backed by this set, so changes in the returned
set are reflected in this set, and vice-versa.
2.
Throws ClassCastException if toElement is not compatible with this
set's comparator (or, if the set has no comparator, if toElement does
not implement Comparable).
3.Throws
NullPointerException, if toElement is null and this set does not
permit null elements
import java.util.*; class NavigableSetHeadSetNullPointer{ 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("Getting the headSet of null"); mySet.headSet(null); } }
Output
Elements in mySet are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Getting the headSet of null Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1266) at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699) at java.util.TreeMap.headMap(TreeMap.java:891) at java.util.TreeSet.headSet(TreeSet.java:338) at java.util.TreeSet.headSet(TreeSet.java:372) at NavigableSetHeadSetNullPointer.main(NavigableSetHeadSetNullPointer.ja va:16)
4.
Throws IllegalArgumentException, if this set itself has a restricted
range, and toElement lies outside the bounds of the range
import java.util.*; class NavigableSetHeadSetIllegalArg{ public static void main(String args[]){ NavigableSet<Integer> mySet = new TreeSet<> (); NavigableSet<Integer> subSet; /* 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"); subSet = mySet.subSet(5, true, 15, true); System.out.println("\nElements in subSet are"); System.out.println(subSet+"\n"); System.out.println("Getting the headSet of 4"); subSet.headSet(4); } }
Output
Elements in mySet are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Elements in subSet are [6, 8, 10, 12, 14] Getting the headSet of 4 Exception in thread "main" java.lang.IllegalArgumentException: toKey out of range at java.util.TreeMap$AscendingSubMap.headMap(TreeMap.java:1719) at java.util.TreeSet.headSet(TreeSet.java:338) at java.util.TreeSet.headSet(TreeSet.java:372) at NavigableSetHeadSetIllegalArg.main(NavigableSetHeadSetIllegalArg.java:22)
No comments:
Post a Comment