89
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
Output:
true
Example 2
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.tutorAspire.JavaCollectionContainsAllExample2.main(JavaCollectionContainsAllExample2.java:13)
Example 3
Output:
Exception in thread "main" java.lang.NullPointerException at java.util.AbstractCollection.containsAll(AbstractCollection.java:317) at com.tutorAspire.JavaCollectionContainsAllExample3.main(JavaCollectionContainsAllExample3.java:14)
Next TopicJava Collection