Friday, September 5, 2025
HomeLanguagesJavaList containsAll() method in Java with Examples

List containsAll() method in Java with Examples

The containsAll() method of List interface in Java is used to check if this List contains all of the elements in the specified Collection. So basically it is used to check if a List contains a set of elements or not.

Syntax:

boolean containsAll(Collection col)

Parameters: This method accepts a mandatory parameter col which is of the type of collection. This is the collection whose elements are needed to be checked if it is present in the List or not.

Return Value: The method returns True if all elements in the collection col are present in the List otherwise it returns False.

Exception: The method throws NullPointerException if the specified collection is NULL.

Below programs illustrates the containsAll() method:

Program 1:




// Java code to illustrate containsAll() method
import java.util.*;
  
public class ListDemo {
    public static void main(String args[])
    {
        // Creating an empty list
        List<String> list = new ArrayList<String>();
  
        // Use add() method to add elements
        // into the List
        list.add("Welcome");
        list.add("To");
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
  
        // Displaying the List
        System.out.println("List: " + list);
  
        // Creating another empty List
        List<String> listTemp = new ArrayList<String>();
  
        listTemp.add("Geeks");
        listTemp.add("4");
        listTemp.add("Geeks");
  
        System.out.println("Are all the contents equal? "
                           + list.containsAll(listTemp));
    }
}


Output:

List: [Welcome, To, Geeks, 4, Geeks]
Are all the contents equal? true

Program 2:




// Java code to illustrate containsAll() method
import java.util.*;
  
public class ListDemo {
    public static void main(String args[])
    {
        // Creating an empty list
        List<Integer> list = new ArrayList<Integer>();
  
        // Use add() method to add elements
        // into the List
        list.add(10);
        list.add(15);
        list.add(30);
        list.add(20);
        list.add(5);
  
        // Displaying the List
        System.out.println("List: " + list);
  
        // Creating another empty List
        List<Integer> listTemp = new ArrayList<Integer>();
  
        listTemp.add(30);
        listTemp.add(15);
        listTemp.add(5);
  
        System.out.println("Are all the contents equal? "
                           + list.containsAll(listTemp));
    }
}


Output:

List: [10, 15, 30, 20, 5]
Are all the contents equal? true

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#containsAll(java.util.Collection)

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS