![]() |
VOOZH | about |
The clear() method of the HashSet class in Java is used to clear all the elements from the HahSet. This method makes the HashSet empty but does not delete the HashSet itself. It simply removes all elements, leaving the set ready for reuse.
void clear()
Key Points:
Example: This example demonstrates how the clear() method removes all the elements from the HashSet.
HashSet before clear(): [1, 2, 3] HashSet After clear(): [] HashSet isEmpty(): true