Java HashSet clear() Method
Example
Remove all the items 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.clear();
System.out.println(cars);
}
}
Definition and Usage
The clear() method removes all the items from the set.
Tip: Use the isEmpty() method to check if a set is empty.
Syntax
public void clear()
Parameter Values
None.