TreeSet clone() method in java

Let’s learn TreeSet clone() method in java.

TreeSet clone() method in java

clone() method of TreeSet class returns a shallow copy of this TreeSet instance.

Syntax:

public Object clone()

Also read – TreeSet in java

Now let’s see example on TreeSet clone() method.

import java.util.TreeSet;
public class TreeSetCloneMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("green");
      ts.add("blue");
      ts.add("yellow");
      ts.add("red");
      ts.add("violet");
      ts.add("orange");
      System.out.println("TreeSet: " + ts);
      // create a new cloned TreeSet
      TreeSet<String> treeCloned = new TreeSet<String>();
      // clone TreeSet using clone() method
      treeCloned = (TreeSet)ts.clone();
      // print cloned TreeSet
      System.out.println("cloned TreeSet: " + treeCloned);
   }
}


Output:

TreeSet: [blue, green, orange, red, violet, yellow]
cloned TreeSet: [blue, green, orange, red, violet, yellow]