Java Collection containsAll() Method with Examples
Last Updated :
24 Oct, 2023
The Collection interface in Java is the fundamental part of the Collection hierarchy. It is found in the java.util package and offers a wide range of built-in classes and methods. One such useful method is containsAll().
Java containsAll() method
The purpose of this containsAll() is to check whether a collection contains all the elements present in another collection or the mentioned collection or not.
Syntax of containsAll()
boolean containsAll(Collection<?> obj)
Parameters: It takes a Collection object as a parameter which is to be checked with the current collection.
return type: It returns boolean (either `true` or `false`)
true: if the collection object contains all the elements as in the current collection.
false: if the collection object elements don’t match with the current collection elements (if there is a single mismatch, it returns false).
The number of elements in the current collection object and the compared collection object need not be the same.
Example of Java containsAll() method
Java containsAll() method can be implemented with any collection some of them are mentioned with example bellow.
Example 1:(Using List)
Below is the implementation of the above method:
Java
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class GFG {
public static void main(String[] args)
{
List<Integer> list1 = new ArrayList<Integer>(
Arrays.asList( 1 , 2 , 3 , 4 , 5 ));
List<Integer> list2 = new ArrayList<Integer>(
Arrays.asList( 1 , 2 , 3 ));
List<Integer> list3 = new ArrayList<Integer>(
Arrays.asList( 1 , 2 , 7 ));
System.out.println( "List1 elements: " + list1);
System.out.println( "List2 elements: " + list2);
System.out.println(
"Is the List1 contains all the elements of List2 ? "
+ list1.containsAll(list2));
System.out.println( "List3 elements: " + list3);
System.out.println(
"Is the List1 contains all the elements of List3 ? "
+ list1.containsAll(list3));
}
}
|
Output
List1 elements: [1, 2, 3, 4, 5]
List2 elements: [1, 2, 3]
Is the List1 contains all the elements of List2 ? true
List3 elements: [1, 2, 7]
Is the List1 contains all the elements of List3 ? false
Example 2:(Using Set)
Below is the implementation of the above method:
Java
import java.io.*;
import java.util.HashSet;
import java.util.Set;
class GFG {
public static void main(String[] args)
{
Set<String> set1 = new HashSet<String>();
set1.add( "carrot" );
set1.add( "apple" );
set1.add( "tomato" );
Set<String> set2 = new HashSet<String>();
set2.add( "carrot" );
set2.add( "apple" );
set2.add( "tomato" );
set2.add( "apple" );
set2.add( "banana" );
Set<String> set3 = new HashSet<String>();
set3.add( "tomato" );
set3.add( "carrot" );
set3.add( "apple" );
set3.add( "apple" );
System.out.println( "unique Set1 elements: " + set1);
System.out.println( "unique Set2 elements: " + set2);
System.out.println(
"Is set1 contains all the elements of set2? "
+ set1.containsAll(set2));
System.out.println( "unique Set3 elements: " + set3);
System.out.println(
"Is set1 contains all the elements of set3? "
+ set1.containsAll(set3));
}
}
|
Output
unique Set1 elements: [apple, tomato, carrot]
unique Set2 elements: [banana, apple, tomato, carrot]
Is set1 contains all the elements of set2? false
unique Set3 elements: [apple, tomato, carrot]
Is se...
Share your thoughts in the comments
Please Login to comment...