Set removeAll() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. Java // Java Program to demosntrates // the working of Set removeAll() import java.util.HashSet; import java.util.Set; public class Geeks { public static void main(String[] args) { // Adding elements in the first set Set<String> s1 = new HashSet<>(); s1.add("Geek1"); s1.add("Geek2"); s1.add("Geek3"); s1.add("Geek4"); // Adding elements in the second set Set<String> s2 = new HashSet<>(); s2.add("Geek3"); s2.add("Geek4"); System.out.println("Set1 before removeAll(): " + s1); // Remove all elements of s2 from s1 s1.removeAll(s2); System.out.println("Set1 after removeAll(): " + s1); } } OutputSet1 before removeAll(): [Geek4, Geek3, Geek2, Geek1] Set1 after removeAll(): [Geek2, Geek1] Syntax of removeAll() Methodboolean 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. Java // Java program to demosntrates that // removeAll() returns boolean value import java.util.HashSet; import java.util.Set; public class Geeks { public static void main(String[] args) { Set<String> s1 = new HashSet<>(); s1.add("Geek1"); s1.add("Geek2"); s1.add("Geek3"); Set<String> s2 = new HashSet<>(); s2.add("Geek2"); s2.add("Geek3"); // Removing elements that // exist in s2 from s1 boolean res1 = s1.removeAll(s2); System.out.println("Set1 after removing Set2 elements from it: " + s1); System.out.println("Was the collection modified? " + res1); } } OutputSet1 after removing Set2 elements from it: [Geek1] Was the collection modified? true Comment More info G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like