Java Collection removeAll() Method
The removeAll() method of Java Collection Interface only removes those elements of the Collection that are contained in the specified collection.
Syntax
Parameters
Here, the parameter ‘c’ represents the collection which contains the elements to be removed from the invoked collection.
Return Value:
The removeAll () method returns a Boolean value ‘true’ if the collection has changed as a result of the call, else it returns ‘false’.
Throws:
UnsupportedOperationException– if the removeAll method is not supported by this collection.
ClassCastException– if the types of one or more elements in this collection are not compatible with the invoked collection.
NullPointerException– if this collection is null or it contains one or more null elements and this collection does not allow null elements.
Example 1
Output:
collection : [1, 2, 3, 4, 5, 6, 7, 8, 9] List of even numbers : [2, 4, 6, 8] Odd numbers : [1, 3, 5, 7, 9]
Example 2
Output:
Total no : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Table of 2 : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Example 3
Output:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920) at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327) at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297) at com.tutorAspire.JavaCollectionRemoveAllExample3.main(JavaCollectionRemoveAllExample3.java:13)