NavigableSet<E>
headSet(E toElement, boolean inclusive)
Returns
a view of the portion of this set whose elements are less than (or
equal to, if inclusive is true) toElement.
import java.util.*; class TreeSetHeadSetFlag{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); /* Add Elements too mySet */ for(int i=10; i>0; i-=2){ mySet.add(i); } System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); for(int i=0; i<12; i+=2){ System.out.println("\nHeadSet for value " + i +" is "); System.out.print("inclusive is false "); System.out.println(mySet.headSet(i, false)); System.out.print("inclusive is true "); System.out.println(mySet.headSet(i, true)); } } }
Output
Elements in mySet are [2, 4, 6, 8, 10] HeadSet for value 0 is inclusive is false [] inclusive is true [] HeadSet for value 2 is inclusive is false [] inclusive is true [2] HeadSet for value 4 is inclusive is false [2] inclusive is true [2, 4] HeadSet for value 6 is inclusive is false [2, 4] inclusive is true [2, 4, 6] HeadSet for value 8 is inclusive is false [2, 4, 6] inclusive is true [2, 4, 6, 8] HeadSet for value 10 is inclusive is false [2, 4, 6, 8] inclusive is true [2, 4, 6, 8, 10]
1. The
returned set is backed by this set, so changes in the returned set
are reflected in this set, and vice-versa.
import java.util.*; class TreeSetHeadSetFlag1{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); NavigableSet<Integer> headSet; /* Add Elements too mySet */ for(int i=10; i>0; i-=2){ mySet.add(i); } headSet = mySet.headSet(10, true); System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); System.out.println("Elements in headSet are"); System.out.println(headSet+"\n"); System.out.println("\nAdding 3 to headSet"); headSet.add(3); System.out.println("Remving 4 from mySet\n"); mySet.remove(4); System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); System.out.println("Elements in headSet are"); System.out.println(headSet+"\n"); } }
Output
Elements in mySet are [2, 4, 6, 8, 10] Elements in headSet are [2, 4, 6, 8, 10] Adding 3 to headSet Remving 4 from mySet Elements in mySet are [2, 3, 6, 8, 10] Elements in headSet are [2, 3, 6, 8, 10]
2. The
returned set will throw an IllegalArgumentException on an attempt to
insert an element outside its range.
import java.util.*; class TreeSetHeadSetFlagIllegal{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); NavigableSet<Integer> headSet; /* Add Elements too mySet */ for(int i=10; i>0; i-=2){ mySet.add(i); } headSet = mySet.headSet(10, true); System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); System.out.println("Elements in headSet are"); System.out.println(headSet+"\n"); System.out.println("\nAdding 11 to headSet"); headSet.add(11); } }
Output
Elements in mySet are [2, 4, 6, 8, 10] Elements in headSet are [2, 4, 6, 8, 10] Adding 11 to headSet Exception in thread "main" java.lang.IllegalArgumentException: key out of range at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1407) at java.util.TreeSet.add(TreeSet.java:255) at TreeSetHeadSetFlagIllegal.main(TreeSetHeadSetFlagIllegal.java:22)
3.
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).
import java.util.*; class TreeSetHeadSet1NullPointer{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); NavigableSet<Integer> headSet; /* Add Elements too mySet */ for(int i=10; i>0; i-=2){ mySet.add(i); } System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); System.out.println("Getting the Head Set upto null"); headSet = mySet.headSet(null, true); } }
Output
Elements in mySet are [2, 4, 6, 8, 10] Getting the Head Set upto 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 TreeSetHeadSet1NullPointer.main(TreeSetHeadSet1NullPointer.java:17)
No comments:
Post a Comment