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 {

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");
		al.add("second");
		al.add("third");

		System.out.println(al.contains("first"));
		System.out.println(al.contains("last"));

	}
}

Output –

true
false

“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 {

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");
		al.add("second");
		al.add("third");
		
		Stack<String> 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));
		

	}
}

Output –

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) {
        for (Object e : c)
            if (!contains(e))
                return false;
        return true;
    }

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 {

		ArrayList<String> al = new ArrayList<String>();

		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));

	}
}

Output –

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 {

		ArrayList<String> al = new ArrayList<String>();

		al = null;

		System.out.println(al.contains(null));

	}
}

Output –

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]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.