Open In App

Java Collection containsAll() Method with Examples

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java Program demomstrate
// Java containsAll() method
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        // define an ArrayList to store [1,2,3,4,5]
        List<Integer> list1 = new ArrayList<Integer>(
            Arrays.asList(1, 2, 3, 4, 5));
  
        // define an ArrayList to store [1,2,3]
        List<Integer> list2 = new ArrayList<Integer>(
            Arrays.asList(1, 2, 3));
  
        // define an ArrayList to store [1,2,7]
        List<Integer> list3 = new ArrayList<Integer>(
            Arrays.asList(1, 2, 7));
  
        // [1,2,3,4,5]
        System.out.println("List1 elements: " + list1);
        // [1,2,3]
        System.out.println("List2 elements: " + list2);
  
        // the below step prints "true" because elements
        // [1,2,3] are present in [1,2,3,4,5]
        System.out.println(
            "Is the List1 contains all the elements of List2 ? "
            + list1.containsAll(list2));
        //[1,2,7]
        System.out.println("List3 elements: " + list3);
  
        // the below step prints "false" because 7 is not
        // present in [1,2,3,4,5]
        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




// Java Program demomstrate
// Java containsAll() method
import java.io.*;
import java.util.HashSet;
import java.util.Set;
  
// Driver Class
class GFG {
      // main function
    public static void main(String[] args)
    {
        // define set1 to store the elements
        // stores ["apple","tomato", "carrot"]
        Set<String> set1 = new HashSet<String>();
        set1.add("carrot");
        set1.add("apple");
        set1.add("tomato");
  
        // define set2 to store the elements
        // stores ["apple", "banana", "carrot", "tomato"]
        Set<String> set2 = new HashSet<String>();
        set2.add("carrot");
        set2.add("apple");
        set2.add("tomato");
        set2.add("apple");
        set2.add("banana");
  
        // define set3 to store the elements
        // stores ["apple", "tomato", "carrot"]
        Set<String> set3 = new HashSet<String>();
        set3.add("tomato");
        set3.add("carrot");
        set3.add("apple");
        set3.add("apple");
  
        // ["apple", "tomato", "carrot"]
        System.out.println("unique Set1 elements: " + set1);
        // ["apple", "banana", "carrot", "tomato"]
        System.out.println("unique Set2 elements: " + set2);
  
        // The below step prints "false" because
        // set1 donot contains the element "banana"
        System.out.println(
            "Is set1 contains all the elements of set2? "
            + set1.containsAll(set2));
  
        // ["apple", "tomato", "carrot"]
        System.out.println("unique Set3 elements: " + set3);
  
        // The below step prints "true" because set1
        // contains all the elements of 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...


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads