Java Collection containsAll() Method

The containsAll() method of Java Collection Interface returns a Boolean value 'true', if this collection contains all the elements in the invoked collection.

Syntax

Parameters

The parameter 'c' represents the collection to be checked for occurrence in this collection.

Throws

ClassCastException- if the types of the elements in the specified collection are not compatible with this collection.

NullPointerException- if the specified collection is null or it contains one or more null elementsl and this collection does not allow null elements.

Return

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

Example 1

Test it Now

Output:

true

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.ArrayDeque.addLast(ArrayDeque.java:249)
	at java.util.ArrayDeque.add(ArrayDeque.java:423)
	at com.javaTpoint.JavaCollectionContainsAllExample2.main(JavaCollectionContainsAllExample2.java:13)

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.AbstractCollection.containsAll(AbstractCollection.java:317)
	at com.javaTpoint.JavaCollectionContainsAllExample3.main(JavaCollectionContainsAllExample3.java:14)
Next TopicJava Collection




Latest Courses