Java HashSet size() Method
Last Updated : 17 Mar 2025
The size() method of Java HashSet class is used to get the number of elements in this HashSet (its cardinality).
Syntax
Following is the declaration of size() method:
Parameter
This method does not accept any parameter.
Returns
The size() method returns the number of elements present in the HashSet.
Exceptions
NA
Compatibility Version
Java 1.2 and above
Example 1
Output:
HashSet: [Vijay, Mohan, Ravi, Ajay] The size of the set is: 4
Example 2
Output:
Example 3
Output:
Size of student database is: 4