Java HashSet removeAll() Method
Example
Remove the items in a list from a set:
import java.util.ArrayList;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
ArrayList<String> soldCars = new ArrayList<String>();
soldCars.add("Volvo");
soldCars.add("Ford");
cars.removeAll(soldCars);
System.out.println(cars);
}
}
Definition and Usage
The removeAll() method removes all the items from the set that are also in the given collection, like a list or another set.
Syntax
public boolean removeAll(Collection<?> items)
Parameter Values
| Parameter | Description |
|---|---|
| items | Required. A collection with the items to remove from the set. |
Technical Details
| Returns: | true if the set changed, and false if it did not. |
|---|