clone() method

clone() method: The clone object javascript method of TreeSet class is used to return a shallow copy of the specified TreeSet in Java.

Object clone();

Its return type is Object. It returns the object of Object class.

import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		for(String name : listOfNames.values())
			System.out.println("Names from list = "+name);
		
		TreeMap<Integer, String> cloneCopy = (TreeMap<Integer, String>) listOfNames.clone();
		for(String name : cloneCopy.values())
			System.out.println("Names from clones list = "+name);
		
	}
}

Output: Names from list = JAVA
Names from list = GOAL
Names from list = RAVI
Names from clones list = JAVA
Names from clones list = GOAL
Names from clones list = RAVI

Leave a Comment