Skip to content

Java Get HashSet Size Example (length)

This example shows how to get HashSet size (HashSet length) in Java using the size method. This example also shows how to check if HashSet is empty using it.

How to get HashSet size in Java using the size method?

The size method of the HashSet class returns the number of elements stored in the HashSet object.

The size method returns an int containing the number of elements contained in the HashSet object. It returns 0 if there are no elements in the set object.

Output

How to check if the HashSet object is empty?

We can get the size of the HashSet object and then compare it with 0 to check if the HashSet is empty as given below.

Output

This example is a part of the Java HashSet Tutorial with Examples.

Please let me know your views in the comments section below.

References:
Java 8 HashSet

About the author

Leave a Reply

Your email address will not be published.