Java Program to Create the Clone of the TreeSet Collection

You are currently viewing Java Program to Create the Clone of the TreeSet Collection

Java program to create the clone of the TreeSet collection

// Java program to create the clone of the 
// TreeSet collection

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree = new TreeSet < Integer > ();

    tree.add(10);
    tree.add(20);
    tree.add(25);
    tree.add(30);

    TreeSet < Integer > cloneTree = (TreeSet < Integer > ) tree.clone();

    System.out.println("TreeSet 'tree': " + tree);
    System.out.println("Cloned TreeSet 'cloneTree': " + cloneTree);
  }
}

Output

TreeSet 'tree': [10, 20, 25, 30]
Cloned TreeSet 'cloneTree': [10, 20, 25, 30]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply