Javatpoint Logo
Javatpoint Logo

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

Test it Now

Output:

HashSet: [Vijay, Mohan, Ravi, Ajay]
The size of the set is: 4

Example 2

Test it Now

Output:

Size of HashSet is: 3

Example 3

Test it Now

Output:

Size of student database is: 4





Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA