Java Collection removeAll() MethodLast Updated : 17 Mar 2025 The removeAll() method of Java Collection Interface only removes those elements of the Collection that are contained in the specified collection. SyntaxParametersHere, 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 1Output: 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 2Output: 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 3Output: 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.tpointtech.JavaCollectionRemoveAllExample3.main(JavaCollectionRemoveAllExample3.java:13) Next TopicJava-Collection |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India