Java TreeSet.clone()

Syntax

TreeSet.clone() has the following syntax.

public Object clone()

Example

In the following code shows how to use TreeSet.clone() method.


// ww  w  .  j  a va  2 s  . c o  m
import java.util.Iterator;
import java.util.TreeSet;

public class Main {
   public static void main(String[] args) {
     TreeSet <Integer> tree = new TreeSet<Integer> ();
     
     tree.add(12);
     tree.add(13);
     tree.add(14);
     
     // cloning tree into clinetree
     TreeSet<Integer> clonetree = (TreeSet)tree.clone();  
 
     // creating iterator
     Iterator<Integer> iterator = clonetree.iterator();
     
     while (iterator.hasNext()){
        System.out.println(iterator.next());
     }
   }    
}

The code above generates the following result.