TreeSet clear() in Java

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

Syntax:

void java.util.TreeSet.clear()

This method removes all of the elements from this set. The set will be empty after this call returns.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.TreeSet;

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

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

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

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

    }
}

Output:

[]


No comments:

Post a Comment