Skip to content

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

Leave a Reply

Your email address will not be published.