The HashSet.removeAll()
method in Java is used to remove all elements in the HashSet
that are also contained in a specified collection. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
removeAll
Method Syntax- Examples
- Removing Elements from Another Collection
- Removing Elements from a List in a HashSet
- Conclusion
Introduction
The HashSet.removeAll()
method is a member of the HashSet
class in Java. It allows you to remove all elements from the HashSet
that are also contained in the specified collection. If the specified collection contains elements that are present in the HashSet
, those elements will be removed.
removeAll Method Syntax
The syntax for the removeAll
method is as follows:
public boolean removeAll(Collection<?> c)
- The method takes a single parameter
c
of typeCollection<?>
, which specifies the collection containing elements to be removed from theHashSet
. - The method returns a boolean value:
true
if theHashSet
changed as a result of the call.false
if theHashSet
did not change (i.e., none of the elements in the specified collection were present in theHashSet
).
Examples
Removing Elements from Another Collection
The removeAll
method can be used to remove all elements from the HashSet
that are also contained in another HashSet
.
Example
import java.util.HashSet;
public class RemoveAllExample {
public static void main(String[] args) {
// Creating two HashSets of Strings
HashSet<String> languages1 = new HashSet<>();
HashSet<String> languages2 = new HashSet<>();
// Adding elements to the first HashSet
languages1.add("Java");
languages1.add("Python");
languages1.add("C");
// Adding elements to the second HashSet
languages2.add("C");
languages2.add("C++");
languages2.add("Python"); // Duplicate element
// Removing all elements from languages1 that are in languages2
boolean changed = languages1.removeAll(languages2);
// Printing the result of removeAll and the HashSet
System.out.println("Did languages1 change? " + changed);
System.out.println("languages1 after removeAll: " + languages1);
}
}
Output:
Did languages1 change? true
languages1 after removeAll: [Java]
Removing Elements from a List in a HashSet
The removeAll
method can also be used to remove elements from a List
in a HashSet
.
Example
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
public class RemoveAllFromListExample {
public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> languages = new HashSet<>();
// Adding elements to the HashSet
languages.add("Java");
languages.add("Python");
languages.add("C");
// Creating a List of Strings
List<String> removeLanguages = new ArrayList<>();
removeLanguages.add("Java");
removeLanguages.add("Ruby"); // Not present in the HashSet
// Removing all elements from the HashSet that are in the List
boolean changed = languages.removeAll(removeLanguages);
// Printing the result of removeAll and the HashSet
System.out.println("Did languages change? " + changed);
System.out.println("languages after removeAll: " + languages);
}
}
Output:
Did languages change? true
languages after removeAll: [C, Python]
Conclusion
The HashSet.removeAll()
method in Java provides a way to remove all elements in the HashSet
that are also contained in a specified collection. By understanding how to use this method, you can efficiently remove multiple elements from your collections. This method is particularly useful for scenarios where you need to clean up or update a set by removing a group of elements that are present in another collection, such as another HashSet
or a List
.