Wednesday, July 3, 2024
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)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments