Home » Java HashSet size() Method with Examples

Java HashSet size() Method with Examples

by Online Tutorials Library

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  

You may also like