Java TreeSet Get Size Example (TreeSet Length)

This example shows how to get TreeSet size using the size method in Java. The TreeSet size or length means the number of elements stored in the TreeSet object.

How to get TreeSet size in Java?

The size method of the TreeSet class returns the size of the TreeSet object.

The size method returns an int value containing the number of elements stored in the set object. It returns 0 when the TreeSet object is empty.

Output

How to check if TreeSet is empty using the size method?

The size method returns 0 if there are no elements in the TreeSet. We can compare the return value of the size method with 0 to check if the TreeSet is empty or not as given below.

Output

Please also visit how to check if TreeSet is empty in Java example to know more.

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

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

References:
Java 8 TreeSet

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