84
Java HashSet clear() Method
The clear() method of Java HashSet class is used to removes all of the elements from this set.
Syntax
Following is the declaration of clear() method:
Parameter
This method does not accept any parameter.
Returns
The clear() method does not return any value.
Exceptions:
NA
Compatibility Version
Java 1.2 and above
Example 1
Output:
Hash set Elements: [DFC, SSSIT, JTP] Hash set elements after clear: []
Example 2
Output:
Hash set Elements: [151, 121, 111] Hash set elements after clear: []
Example 3
Output:
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
Next TopicHashSet clone() Method