Java HashSet remove() Method
Example
Remove an item from a set:
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");
cars.remove("Volvo");
System.out.println(cars);
}
}
Definition and Usage
The remove() method removes an item from the set.
If the item is not in the set, nothing happens, and the method returns false.
Syntax
public boolean remove(Object item)
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to remove. |
Technical Details
| Returns: | true if the item was in the set and was removed, and false if it was not in the set. |
|---|