boolean
containsAll(Collection<?> c)
Returns
true if this set contains all of the elements of the specified
collection.
import java.util.*; class TreeSetContainsAll{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); List<Integer> myList = new ArrayList<> (); /* Add Elements to the Set */ for(int i=0; i < 20; i++) mySet.add(i); /* Add Elements to the List */ for(int i=10; i<15; i++) myList.add(i); System.out.println("Elements in the Set are"); System.out.println(mySet); System.out.println("\nElements in the list are"); System.out.println(myList); /* Search whether the mySet contains all the Elements of the myList */ if(mySet.containsAll(myList)){ System.out.println("Set contains all the Elements of the list"); } else{ System.out.println("Set doesn't contain all the Elements of the list"); } /* Add 100 to the myList */ myList.add(100); System.out.println("\nElements in the list are"); System.out.println(myList); /* Search whether the mySet contains all the Elements of the myList */ if(mySet.containsAll(myList)){ System.out.println("Set contains all the Elements of the list"); } else{ System.out.println("Set doesn't contain all the Elements of the list"); } } }
Output
Elements in the Set are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Elements in the list are [10, 11, 12, 13, 14] Set contains all the Elements of the list Elements in the list are [10, 11, 12, 13, 14, 100] Set doesn't contain all the Elements of the list
containsAll
takes any Collection as parameter, so mySet takes myList as a
parameter and verifies whether all the elements in the list are
present in the set or not.
1.
ContainsAll() throws NullPointerException if the specified collection
contains one or more null elements and this set does not permit null
elements.
import java.util.*; class TreeSetContainsAllNull{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); List<Integer> myList = new ArrayList<> (); /* Add Elements to the Set */ for(int i=0; i < 20; i++) mySet.add(i); myList.add(null); System.out.println("Elements in the Set are"); System.out.println(mySet); System.out.println("\nElements in the list are"); System.out.println(myList); /* Search whether the mySet contains all the Elements of the myList */ if(mySet.containsAll(myList)){ System.out.println("Set contains all the Elements of the list"); } else{ System.out.println("Set doesn't contain all the Elements of the list"); } } }
Output
Elements in the Set are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Elements in the list are [null] Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.getEntry(TreeMap.java:342) at java.util.TreeMap.containsKey(TreeMap.java:227) at java.util.TreeSet.contains(TreeSet.java:234) at java.util.AbstractCollection.containsAll(AbstractCollection.java:308) at TreeSetContainsAllNull.main(TreeSetContainsAllNull.java:20)
2. containsAll()
throws “NullPointerException” if the specified collection is
null.
import java.util.*; class TreeSetContainsAllNullEx{ public static void main(String args[]){ Set<Integer> mySet = new TreeSet<> (); List<Integer> myList = null; /* Add Elements to the Set */ for(int i=0; i < 20; i++) mySet.add(i); System.out.println("Elements in the Set are"); System.out.println(mySet); System.out.println("\nElements in the list are"); System.out.println(myList); /* Search whether the mySet contains all the Elements of the myList */ if(mySet.containsAll(myList)){ System.out.println("Set contains all the Elements of the list"); } else{ System.out.println("Set doesn't contain all the Elements of the list"); } } }
Output
Elements in the Set are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Elements in the list are null Exception in thread "main" java.lang.NullPointerException at java.util.AbstractCollection.containsAll(AbstractCollection.java:307) at TreeSetContainsAllNullEx.main(TreeSetContainsAllNullEx.java:18)
3. throws
ClassCastException if the types of one or more elements in the
specified collection are incompatible with this set.
import java.util.*; class TreeSetContainsAllClassCast{ public static void main(String args[]){ Set<Integer> mySet = new TreeSet<> (); List<Double> myList = new ArrayList<Double> (); /* Add Elements to the Set */ for(int i=0; i < 20; i++) mySet.add(i); myList.add(1001.01); System.out.println("Elements in the Set are"); System.out.println(mySet); System.out.println("\nElements in the list are"); System.out.println(myList); /* Search whether the mySet contains all the Elements of the myList */ if(mySet.containsAll(myList)){ System.out.println("Set contains all the Elements of the list"); } else{ System.out.println("Set doesn't contain all the Elements of the list"); } } }
Output
Elements in the Set are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Elements in the list are [1001.01] Exception in thread "main" java.lang.ClassCastException: java.lang.Integer canno t be cast to java.lang.Double at java.lang.Double.compareTo(Double.java:49) at java.util.TreeMap.getEntry(TreeMap.java:346) at java.util.TreeMap.containsKey(TreeMap.java:227) at java.util.TreeSet.contains(TreeSet.java:234) at java.util.AbstractCollection.containsAll(AbstractCollection.java:308) at TreeSetContainsAllClassCast.main(TreeSetContainsAllClassCast.java:20)
No comments:
Post a Comment