Home » Java List containsAll() Method with Examples

Java List containsAll() Method with Examples

by Online Tutorials Library

Java List containsAll() Method

The containsAll() method of List interface returns a Boolean value ‘true’ if this list contains all the elements of the invoked collection.

Syntax

Parameters

The parameter ‘c’ represents the collection to be checked for occurrence in this list.

Specified By

containsAll in interface Collection<E>

Throws:

ClassCastException– If the types of one or more elements in the specified collection are not compatible with this list.

NullPointerException– If the specified collection is null or it contains one or more null elements, and this list does not allow null elements.

Returns:

The containsAll() method returns Boolean value ‘true’ if this list contains all the elements in the invoked collection, else it returns false.

Example 1

Test it Now

Output:

Total elements in the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  Contains All method will return true  

Example 2

Test it Now

Output:

Total elements in the list : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  Exception in thread "main" java.lang.NullPointerException  at java.util.AbstractCollection.containsAll(AbstractCollection.java:317)  at com.tutorAspire.JavaListContainsAllExample2.main(JavaListContainsAllExample2.java:13)  

Example 3

Test it Now

Output:

Total elements in the list : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  Total elements in the list1 : [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]  Contains All method will return false  

Next TopicJava List

You may also like