- Java ArrayList containsAll()
- Example 1: Java ArrayList containsAll()
- Example 2: containsAll() Between Java ArrayList and HashSet
- ArrayList contains() and ContainsAll() method in Java
- public boolean contains(Object o)
- public boolean containsAll(Collection c)
- The What If scenarios
- What is the ArrayList.containsAll() method in Java?
- Syntax
- Return value
- Examples
- Example 1
- Explanation
- Example 2
- Explanation
- Contains all method in java
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
Java ArrayList containsAll()
Note: We can say that the containsAll() method checks if the collection is a subset of the arraylist.
Example 1: Java ArrayList containsAll()
import java.util.ArrayList; class Main < public static void main(String[] args) < // create an ArrayList ArrayListlanguages1 = new ArrayList<>(); // insert element to the ArrayList languages1.add("JavaScript"); languages1.add("Python"); languages1.add("Java"); System.out.println("ArrayList 1: " + languages1); // create another ArrayList ArrayList languages2 = new ArrayList<>(); // add elements to ArrayList languages2.add("Java"); languages2.add("Python"); System.out.println("ArrayList 2: " + languages2); // check if ArrayList 1 contains ArrayList 2 boolean result1 = languages1.containsAll(languages2); System.out.println("ArrayList 1 contains all elements of ArrayList 2: " + result1); // check if ArrayList 2 contains ArrayList 1 boolean result2 = languages2.containsAll(languages1); System.out.println("ArrayList 2 contains all elements of ArrayList 1: " + result2); > >
ArrayList 1: [JavaScript, Python, Java] ArrayList 2: [Java, Python] ArrayList 1 contains all elements of ArrayList 2: true ArrayList 2 contains all elements of ArrayList 1: false
In the above example, we have created two arraylists named languages1 and languages2 . Notice the expression,
// return true languages1.containsAll(languages2)
Here, the containsAll() method checks if languages1 contains all elements of languages2 . Hence, the method returns true . However, notice the following expression,
// return false languages2.containsAll(languages1)
Here, the containsAll() method checks if languages2 contains all elements of languages1 . Hence, it returns false .
Note: The containsAll() method is not specific to the ArrayList class. The class inherits from the List interface.
Example 2: containsAll() Between Java ArrayList and HashSet
import java.util.ArrayList; import java.util.HashSet; class Main < public static void main(String[] args) < // create an ArrayList ArrayListnumbers = new ArrayList<>(); // add element to ArrayList numbers.add(1); numbers.add(2); numbers.add(3); System.out.println("ArrayList: " + numbers); // create a HashSet HashSet primeNumbers = new HashSet<>(); // add elements to HashSet primeNumbers.add(2); primeNumbers.add(3); System.out.println("HashSet: " + primeNumbers); // check if ArrayList contains all elements of HashSet boolean result1 = numbers.containsAll(primeNumbers); System.out.println("ArrayList contains all elements of HashSet: " + result1); // check if HashSet contains all elements of ArrayList boolean result2 = primeNumbers.containsAll(numbers); System.out.println("HashSet contains all elements of ArrayList: " + result2); > >
ArrayList: [1, 2, 3] HashSet: [2, 3] ArrayList contains all elements of HashSet: true HashSet contains all elements of ArrayList: false
In the above example, we have created an arraylist named numbers and a hashset named primeNumbers . Notice the expressions,
// check if ArrayList contains HashSet // return true numbers.containsAll(primeNumbers) // check if HashSet contains ArrayList // return false primeNumbers.containsAll(numbers)
Note: We can get the common elements between ArrayList and HashSet using the Java ArrayList retainAll() method.
ArrayList contains() and ContainsAll() method in Java
This post will discuss the contains() and containsAll() methods implemented in the ArrayList class . They are used to check whether an ArrayList contains a specified element/Collection or not.
Let’s discuss them one by one.
public boolean contains(Object o)
- What does it do? It will check whether the list contains the specified element or not, which we have passed in the function’s argument.
- What does it return? It will return true if the specified element is present in the ArrayList. Otherwise, it will return false.
Code Example
public class Codekru < public static void main(String[] args) throws Exception < ArrayListal = new ArrayList(); al.add("first"); al.add("second"); al.add("third"); System.out.println(al.contains("first")); System.out.println(al.contains("last")); > >
“first” was present in the ArrayList, so the contains() method returned true for the same while it returned false for the string “last”, as it was not present within the ArrayList.
Internal Implementation of the ArrayList contains() method
public boolean contains(Object o) < return indexOf(o) >= 0; >
Here, we can see that the contains() method internally uses the indexOf() method to check whether the specified element is present within the list or not. And if the indexOf() method returns a value that is greater than or equal to zero ( >=0 ), then only the contains() method will return true. Otherwise, it will return false.
Time complexity of the ArrayList contains() method
After seeing the internal implementation of the contains() method, we know that the time complexity of the contains() methods is dependent on the indexOf() method. As the average time complexity of the indexOf() method is O(n) so, the average time complexity of the contains() method would also be O(n).
public boolean containsAll(Collection c)
- What does it do? It will check whether the list contains the specified collection or not, which we have passed in the function’s argument.
- What does it return? It will return true if the specified collection is present in the ArrayList. Otherwise, it will return false.
We can take any class that implements the Collection interface and pass it in the containsAll() method’s argument to check whether the ArrayList contains all the elements of that collection or not.
So, let’s take a stack and find out whether the ArrayList contains the stack’s elements.
Code Example
public class Codekru < public static void main(String[] args) throws Exception < ArrayListal = new ArrayList(); al.add("first"); al.add("second"); al.add("third"); Stack stack = new Stack<>(); stack.add("first"); stack.add("second"); System.out.println("Does list contains all of stack elements? "+al.containsAll(stack)); stack.add("last"); // this element is not presen in the list System.out.println("Does list contains all of stack elements? "+al.containsAll(stack)); > >
Does list contains all of stack elements? true Does list contains all of stack elements? false
Here, the method returned true as the stack contained only two strings ( “first” and “second” ) that was also present in the ArrayList, but when we added a third string ( “last” ) which was present in the ArrayList, then the function returned false.
Internal Implementation of the ArrayList containsAll() method
public boolean containsAll(Collection c)
Here, we can see that the containsAll() method internally uses the contains() method to check whether the individual elements of the collection are present within the ArrayList or not. And if any single element from the collection is not present within the ArrayList, then the containsAll() method will return false and not iterate any further.
Time complexity of the ArrayList containsAll() method
As we saw, that containsAll() method internally iterates over all of the collection elements and uses contains() method in each iteration, which further iterates over the list elements.
So, the average time complexity of the containsAll() method will be O(nm), where n is the size of the ArrayList and m is the size of the collection passed in the argument.
The What If scenarios
Q – What if we want to find the null object using contains() method? Can we do that?
Yes, we can do that. We can find the null object using the contains() method, and if any null object is present in the list, then it will return true, as shown in the below program.
public class Codekru < public static void main(String[] args) throws Exception < ArrayListal = new ArrayList(); al.add("first"); al.add(null); al.add("third"); System.out.println("ArrayList contents: " + al); System.out.println("Does arraylist contains null object? " + al.contains(null)); > >
ArrayList contents: [first, null, third] Does arraylist contains null object? true
Q – What if we want to use the contains() method on a null ArrayList?
It will throw NullPointerException as illustrated by the below program.
public class Codekru < public static void main(String[] args) throws Exception < ArrayListal = new ArrayList(); al = null; System.out.println(al.contains(null)); > >
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.contains(Object)" because "al" is null
Please visit this link to learn more about the ArrayList class of java and its other functions or methods.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].
What is the ArrayList.containsAll() method in Java?
Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.
The ArrayList.containsAll() method in Java is used to check if the list contains all the elements present in a collection.
Syntax
The ArrayList.containsAll() method in Java can be declared as shown in the code snippet below:
boolean containsAll(Collection c)
- c : The collection whose elements will be checked if they are present in the list.
Return value
The ArrayList.containsAll() method returns a boolean , such that:
- The return value is true if the list contains all the elements present in the collection c .
- The return value is false if the list does not contain all the elements present in the collection c .
Note: If the collection is null , the ArrayList.containsAll() method throws the NullPointerException .
Examples
Example 1
Consider the code snippet below, which demonstrates the use of the ArrayList.containsAll() method.
import java.util.*;public class Example1public static void main(String args[])List list1 = new ArrayList();list1.add("Hello");list1.add("world");list1.add("!");System.out.println("list1: " + list1);List list2 = new ArrayList();list2.add("Hello");list2.add("world2");System.out.println("list2: " + list2);System.out.println("list1.containsAll(): " + list1.containsAll(list2));>>Explanation
Two lists, list1 and list2 , are declared in line 7 and line 13 respectively. The ArrayList.containsAll() method is used in line 18 to check if all the elements of list2 are present in list1 . The ArrayList.containsAll() method returns false , which means that list1 does not contain all the elements in list2 .
Example 2
Consider another example of the ArrayList.containsAll() method.
import java.util.*;public class Example2public static void main(String args[])List list1 = new ArrayList();list1.add("Hello");list1.add("world");list1.add("!");System.out.println("list1: " + list1);List list2 = new ArrayList();list2.add("Hello");list2.add("world");System.out.println("list2: " + list2);System.out.println("list1.containsAll(): " + list1.containsAll(list2));>>Explanation
Two lists, list1 and list2 , are declared in line 7 and line 13 respectively. The ArrayList.containsAll() method is used in line 18 to check if all the elements of list2 are present in list1 . The ArrayList.containsAll() method returns true , which means that list1 contains all the elements in list2 .
Learn in-demand tech skills in half the time
Contains all method in java
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 weekLike/Subscribe us for latest updates or newsletter