VOOZH about

URL: https://www.geeksforgeeks.org/java/set-removeall-method-in-java-with-examples/

⇱ Set removeAll() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Set removeAll() Method in Java

Last Updated : 11 Jul, 2025

In Java, the removeAll() method is part of the Collection interface. It is used to remove all elements from a collection that are present in another collection.

Example 1: This example demonstrates how the removeAll() method removes all elements from the first set that are also present in the second set.


Output
Set1 before removeAll(): [Geek4, Geek3, Geek2, Geek1]
Set1 after removeAll(): [Geek2, Geek1]

Syntax of removeAll() Method

boolean removeAll(Collection<?> c)

  • Parameter: The collection "c" contains the elements to be removed from the calling collection. If an element in c matches any element in the original collection, it will be removed.
  • Return Type: This method return "true" if elements are removed, otherwise it return "false".

Example 2: This example shows how removeAll() returns a boolean indicating whether any elements were removed from the first set by comparing it to the second set.


Output
Set1 after removing Set2 elements from it: [Geek1]
Was the collection modified? true
Comment