Thursday 20 March 2014

Search collection in a Set

boolean containsAll(Collection<?> c)
   Returns true if this set contains all of the elements of the specified collection.

import java.util.*;
class SetContainsAll{
 public static void main(String args[]){
  Set<Integer> mySet = new HashSet<> ();
  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, 17, 16, 19, 18]

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 SetContainsAllNull{
 public static void main(String args[]){
  Set<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(Unknown Source)
        at java.util.TreeMap.containsKey(Unknown Source)
        at java.util.TreeSet.contains(Unknown Source)
        at java.util.AbstractCollection.containsAll(Unknown Source)
        at SetContainsAllNull.main(SetContainsAllNull.java:20)

2.containsAll() throws “NullPointerException” if the specified collection is null.

import java.util.*;
class SetContainsAllNullEx{
 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(Unknown Source)
        at SetContainsAllNullEx.main(SetContainsAllNullEx.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 SetContainsAllClassCast{
 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");
  }
  
 }
}

mySet is of type Integer and myList is of type Double which are not comparable, so at run time, classCastException thrown.

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(Unknown Source)
        at java.util.TreeMap.getEntry(Unknown Source)
        at java.util.TreeMap.containsKey(Unknown Source)
        at java.util.TreeSet.contains(Unknown Source)
        at java.util.AbstractCollection.containsAll(Unknown Source)
        at SetContainsAllClassCast.main(SetContainsAllClassCast.java:20)



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment