Get size of TreeSet in Java

In this tutorial, we will learn how to Get size of TreeSet in Java.

TreeSet in Java extends AbstractSet and implements the NavigableSet interface. It creates a collection that uses a tree for storage. The objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly. TreeSet is similar to HashSet, the difference is that TreeSet sorts the elements in ascending order and doesn’t allow null elements.

In we want to calculate the size or no. of elements in TreeSet , then this class in Java provides size() method to calculate the size. The size method returns the size or the no. of elements in TreeSet. The syntax of size() method is:

Syntax of size() method:

the number of elements in this set.



In the above program, we have used size() method to calculate the size of TreeSet n Java.

Related posts

Leave a Comment