TreeSet size() in Java

size(): This method is available in java.util.TreeSet class of Java.

Syntax:

int java.util.TreeSet.size()

This method returns the number of elements in this set (its cardinality).

Parameters: NA

Returns: the number of elements in this set (its cardinality).

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetsize {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        System.out.println(treeSet.size());

    }
}

Output:

4


No comments:

Post a Comment