In this shot, we will discuss how to use the TreeSet.size()
method in Java. The TreeSet.size()
method is present in the TreeSet
class inside the java.util
package and is used to obtain the number of elements in the TreeSet or the size of the TreeSet.
The TreeSet.size()
method does not take any parameters.
TreeSet.size()
returns the number of elements present in or the size of the TreeSet.
We have a TreeSet =[1,8,5,3,9]
The number of elements in the TreeSet = 5
The result of the TreeSet.size()
method is 5
Let’s have a look at the code snippet.
import java.io.*;import java.util.TreeSet;class Main{public static void main(String args[]){TreeSet<Integer> tree_set = new TreeSet<Integer>();tree_set.add(1);tree_set.add(8);tree_set.add(5);tree_set.add(3);tree_set.add(0);System.out.println("TreeSet: " + tree_set.size());}}
Main
class.main
function.Integer
type.TreeSet.add()
method.TreeSet.size()
method to get the size of TreeSet and display the result with a message.