The containsAll() method of Java LinkedHashSet 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 LinkedHashSet.containsAll() method:
Program 1:
import java.util.*;
class LinkedHashSetDemo {
public static void main(String args[])
{
LinkedHashSet<String>
set = new LinkedHashSet<String>();
set.add( "Geeks" );
set.add( "for" );
set.add( "Geeks" );
set.add( "10" );
set.add( "20" );
System.out.println( "LinkedHashSet 1: "
+ set);
LinkedHashSet<String>
set2 = new LinkedHashSet<String>();
set2.add( "Geeks" );
set2.add( "for" );
set2.add( "Geeks" );
set2.add( "10" );
set2.add( "20" );
System.out.println( "LinkedHashSet 2: "
+ set2);
System.out.println( "\nDoes set 1 contains set 2: "
+ set.containsAll(set2));
}
}
|
Output:
LinkedHashSet 1: [Geeks, for, 10, 20]
LinkedHashSet 2: [Geeks, for, 10, 20]
Does set 1 contains set 2: true
Program 2:
import java.util.*;
class LinkedHashSetDemo {
public static void main(String args[])
{
LinkedHashSet<String>
set = new LinkedHashSet<String>();
set.add( "Geeks" );
set.add( "for" );
set.add( "Geeks" );
System.out.println( "LinkedHashSet 1: "
+ set);
LinkedHashSet<String>
set2 = new LinkedHashSet<String>();
set2.add( "10" );
set2.add( "20" );
System.out.println( "LinkedHashSet 2: "
+ set2);
System.out.println( "\nDoes set 1 contains set 2: "
+ set.containsAll(set2));
}
}
|
Output:
LinkedHashSet 1: [Geeks, for]
LinkedHashSet 2: [10, 20]
Does set 1 contains set 2: false
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Dec, 2018
Like Article
Save Article