The containsAll() method of Java AbstractSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set.
Syntax:
public boolean containsAll(Collection C)
Parameters: The parameter C is a Collection. This parameter refers to the set whose elements occurrence is needed to be checked in this set.
Return Value: The method returns True if this set contains all the elements of other set otherwise it returns False.
Below programs illustrate the AbstractSet.conatinsAll() method:
Program 1:
import java.util.*;
class AbstractSetDemo {
public static void main(String args[])
{
AbstractSet<String>
abs = new TreeSet<String>();
abs.add( "Geeks" );
abs.add( "for" );
abs.add( "Geeks" );
abs.add( "10" );
abs.add( "20" );
System.out.println( "AbstractSet 1: "
+ abs);
AbstractSet<String>
abs2 = new TreeSet<String>();
abs2.add( "Geeks" );
abs2.add( "for" );
abs2.add( "Geeks" );
abs2.add( "10" );
abs2.add( "20" );
System.out.println( "AbstractSet 2: "
+ abs2);
System.out.println( "\nDoes set 1 contains set 2: "
+ abs.containsAll(abs2));
}
}
|
Output:
AbstractSet 1: [10, 20, Geeks, for]
AbstractSet 2: [10, 20, Geeks, for]
Does set 1 contains set 2: true
Program 2:
import java.util.*;
class AbstractSetDemo {
public static void main(String args[])
{
AbstractSet<String>
abs = new TreeSet<String>();
abs.add( "Geeks" );
abs.add( "for" );
abs.add( "Geeks" );
System.out.println( "AbstractSet 1: "
+ abs);
AbstractSet<String>
abs2 = new TreeSet<String>();
abs2.add( "10" );
abs2.add( "20" );
System.out.println( "AbstractSet 2: "
+ abs2);
System.out.println( "\nDoes set 1 contains set 2: "
+ abs.containsAll(abs2));
}
}
|
Output:
AbstractSet 1: [Geeks, for]
AbstractSet 2: [10, 20]
Does set 1 contains set 2: false