99
Java HashSet size() Method
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:
Size of HashSet is: 3
Example 3
Output:
Size of student database is: 4
Next TopicHashSet spliterator() Method