Java HashSet clear() Method
The clear() method of Java HashSet class is used to removes all of the elements from this set.
Following is the declaration of clear() method:
This method does not accept any parameter.
The clear() method does not return any value.
Java 1.2 and above
Example 1Test it Now
Hash set Elements: [DFC, SSSIT, JTP] Hash set elements after clear: 
Example 2Test it Now
Hash set Elements: [151, 121, 111] Hash set elements after clear: 
Example 3Test it Now
Size of the HashSet: 4 Elements of the HashSet before clear: Google Twitter Instagram Facebook Elements of the HashSet after clear: Size of the hashset: 0