SortedSet containsAll() method in Java with Examples
Last Updated :
30 Sep, 2019
The containsAll() method of Java SortedSet 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.
Note: The containsAll() method in SortedSet is inherited from the Set interface in Java.
Below programs illustrate the Set.containsAll() method:
Program 1:
import java.util.*;
class SortedSetDemo {
public static void main(String args[])
{
SortedSet<String>
set = new TreeSet<String>();
set.add( "Geeks" );
set.add( "for" );
set.add( "Geeks" );
set.add( "10" );
set.add( "20" );
System.out.println( "Set 1: "
+ set);
Set<String>
set2 = new HashSet<String>();
set2.add( "Geeks" );
set2.add( "for" );
set2.add( "Geeks" );
set2.add( "10" );
set2.add( "20" );
System.out.println( "Set 2: "
+ set2);
System.out.println(
"\nDoes set 1 contains set 2?: "
+ set.containsAll(set2));
}
}
|
Output:
Set 1: [10, 20, Geeks, for]
Set 2: [Geeks, for, 20, 10]
Does set 1 contains set 2?: true
Program 2:
import java.util.*;
class SortedSetDemo {
public static void main(String args[])
{
SortedSet<String>
set = new TreeSet<String>();
set.add( "Geeks" );
set.add( "for" );
set.add( "Geeks" );
System.out.println( "Set 1: "
+ set);
Set<String>
set2 = new HashSet<String>();
set2.add( "10" );
set2.add( "20" );
System.out.println( "Set 2: "
+ set2);
System.out.println(
"\nDoes set 1 contains set 2: "
+ set.containsAll(set2));
}
}
|
Output:
Set 1: [Geeks, for]
Set 2: [20, 10]
Does set 1 contains set 2: false
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#containsAll(java.util.Collection)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...