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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping