TreeSet clone() in Java

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

Syntax:

Object java.util.TreeSet.clone()

This method returns a shallow copy of this TreeSet instance.

Parameters: NA

Returns: a shallow copy of this set.

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetclone {
    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.clone());

    }
}

Output:

[C++, Hello, Java, Program]


No comments:

Post a Comment