SortedSet<E>
tailSet(E fromElement)
Returns
a view of the portion of this set whose elements are greater than or
equal to fromElement.
import java.util.*; class SortedSetTailSet{ public static void main(String args[]){ SortedSet<Integer> mySet1 = new TreeSet<>(); SortedSet<Integer> tailSet; /* Add Elements to mySet1 */ for(int i=20; i > 0; i-=2) mySet1.add(i); System.out.println("Elements in mySet1 are"); System.out.println(mySet1); System.out.println("\nget the tailSet from 10"); tailSet = mySet1.tailSet(10); System.out.println("\nElements in tailSet are"); System.out.println(tailSet); } }
Output
Elements in mySet1 are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] get the tailSet from 10 Elements in tailSet are [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.
import java.util.*; class SortedSetTailSet1{ public static void main(String args[]){ SortedSet<Integer> mySet1 = new TreeSet<>(); SortedSet<Integer> tailSet; /* Add Elements to mySet1 */ for(int i=20; i > 0; i-=2) mySet1.add(i); System.out.println("Elements in mySet1 are"); System.out.println(mySet1); System.out.println("\nget the tailSet from 10"); tailSet = mySet1.tailSet(10); System.out.println("\nElements in tailSet are"); System.out.println(tailSet); /* Adding 11,13,15,17,19 to tailSet */ tailSet.add(11); tailSet.add(13); tailSet.add(15); tailSet.add(17); tailSet.add(19); /* Adding 12,14,16,18,20 to mySet1 */ mySet1.remove(12); mySet1.remove(14); mySet1.remove(16); mySet1.remove(18); mySet1.remove(20); System.out.println("\nElements in mySet1 are"); System.out.println(mySet1); System.out.println("\nElements in tailSet are"); System.out.println(tailSet); } }
Output
Elements in mySet1 are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] get the tailSet from 10 Elements in tailSet are [10, 12, 14, 16, 18, 20] Elements in mySet1 are [2, 4, 6, 8, 10, 11, 13, 15, 17, 19] Elements in tailSet are [10, 11, 13, 15, 17, 19]
2.
throws IllegalArgumentException if this set itself has a restricted
range, and fromElement lies outside the bounds of the range
import java.util.*; class SortedSetTailSetIllegal{ public static void main(String args[]){ SortedSet<Integer> mySet1 = new TreeSet<>(); SortedSet<Integer> subSet; SortedSet<Integer> tailSet; /* Add Elements to mySet1 */ for(int i=20; i > 0; i-=2) mySet1.add(i); subSet = mySet1.subSet(10, 15); System.out.println("Elements in subSet are"); System.out.println(subSet); System.out.println("Get the tailSet of subSet from 16"); tailSet = subSet.tailSet(16); } }
Output
Elements in subSet are [10, 12, 14] Get the tailSet of subSet from 16 Exception in thread "main" java.lang.IllegalArgumentException: fromKey out of range at java.util.TreeMap$AscendingSubMap.tailMap(TreeMap.java:1727) at java.util.TreeSet.tailSet(TreeSet.java:350) at java.util.TreeSet.tailSet(TreeSet.java:383) at SortedSetTailSetIllegal.main(SortedSetTailSetIllegal.java:19)
3.
throws NullPointerException if fromElement is null and this set does
not permit null elements
import java.util.*; class SortedSetTailSetNullPointer{ public static void main(String args[]){ SortedSet<Integer> mySet1 = new TreeSet<>(); SortedSet<Integer> tailSet; /* Add Elements to mySet1 */ for(int i=20; i > 0; i-=2) mySet1.add(i); tailSet = mySet1.tailSet(null); } }
Output
Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1264) at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699) at java.util.TreeMap.tailMap(TreeMap.java:905) at java.util.TreeSet.tailSet(TreeSet.java:350) at java.util.TreeSet.tailSet(TreeSet.java:383) at SortedSetTailSetNullPointer.main(SortedSetTailSetNullPointer.java:12)
4.
throws ClassCastException if fromElement is not compatible with this
set's comparator
class Employee{ }
import java.util.*; class SortedSetTailSetClassCast{ public static void main(String args[]){ SortedSet<Employee> mySet1 = new TreeSet<>(); SortedSet<Employee> tailSet; Employee e1 = new Employee(); tailSet = mySet1.tailSet(e1); } }
Output
Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1264) at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699) at java.util.TreeMap.tailMap(TreeMap.java:905) at java.util.TreeSet.tailSet(TreeSet.java:350) at java.util.TreeSet.tailSet(TreeSet.java:383) at SortedSetTailSetClassCast.main(SortedSetTailSetClassCast.java:10)
No comments:
Post a Comment