Home » Java HashSet clear() Method with Examples

Java HashSet clear() Method with Examples

by Online Tutorials Library

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

Test it Now

Output:

Hash set Elements: [DFC, SSSIT, JTP]  Hash set elements after clear: []  

Example 2

Test it Now

Output:

Hash set Elements: [151, 121, 111]  Hash set elements after clear: []  

Example 3

Test it Now

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  

You may also like