Set removeAll() Method in Java Last Updated : 06 Feb, 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 infoAdvertise with us Next Article Set removeAll() Method in Java gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Practice Tags : JavaJava-Collections Similar Reads Set in Java The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat 14 min read Set add() method in Java with Examples The add() method of Set in Java is used to add a specific element into a Set collection. The set add() function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Declaration of add() methodbo 2 min read Set contains() method in Java with Examples The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme 2 min read Set remove() method in Java with Examples The java.util.Set.remove(Object O) method is used to remove a particular element from a Set. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this Set and specifies the element to be removed from the Set. Return Value: This method returns True if t 1 min read Set addAll() Method in Java In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order.Example 1: This example demonstrates how to merge two TreeSet using the addAll() method.Java// Java 2 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read Set containsAll() Method in Java The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface. Example 1: This example checks if all elements of one set are present in another set and it will return true if they are iden 2 min read Set hashCode() Method in Java In Java, the hashCode() method is defined in the Object class and is used to generate a hash code for objects. It plays a very important role in hash-based collections like HashMap, HashSet, and HashTable. Example 1: This example demonstrates how hashCode() is used to get the hash code of the HashSe 2 min read Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Set removeAll() Method in Java 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 2 min read Like