java.util.TreeSet.clone() Method


Advertisements


Description

The clone() method is used to return a shallow copy of this TreeSet instance.

Declaration

Following is the declaration for java.util.TreeSet.clone() method.

public Object clone()

Parameters

NA

Return Value

The method call returns a shallow copy of this set.

Exception

NA

Example

The following example shows the usage of java.util.TreeSet.clone() method.

Live Demo
package com.tutorialspoint;

import java.util.Iterator;
import java.util.TreeSet;

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

      // creating TreeSet 
      TreeSet <Integer>tree = new TreeSet<Integer>();
      TreeSet <Integer>clonetree = new TreeSet<Integer>();

      // adding in the tree
      tree.add(12);
      tree.add(13);
      tree.add(14);

      // cloning tree into clinetree
      clonetree = (TreeSet)tree.clone();  
    
      // creating iterator
      Iterator iterator;
      iterator = clonetree.iterator();

      // displaying the clonetree data
      System.out.println("Tree set data: ");     
      
      while (iterator.hasNext()) {
         System.out.println(iterator.next() + " ");
      }
   }    
}

Let us compile and run the above program, this will produce the following result.

Tree set data: 
12 
13 
14 

java_util_treeset.htm


Advertisements
E-Books Store