TreeMap clone() Method in Java with Examples
The clone()
method in Java creates a shallow copy of the TreeMap object. It means that the new TreeMap object will have the same key-value pairs as the original TreeMap object, but any changes made to the values of the new TreeMap object will not affect the original TreeMap object.
Syntax:
public Object clone()
Example 1: Using clone() method to create a shallow copy of a TreeMap object
import java.util.TreeMap;
public class TreeMapCloneExample {
public static void main(String[] args) {
// Creating a TreeMap object
TreeMap<Integer, String> originalTreeMap = new TreeMap<>();
originalTreeMap.put(1, "One");
originalTreeMap.put(2, "Two");
originalTreeMap.put(3, "Three");
originalTreeMap.put(4, "Four");
originalTreeMap.put(5, "Five");
// Creating a shallow copy of the original TreeMap object
TreeMap<Integer, String> clonedTreeMap = (TreeMap<Integer, String>) originalTreeMap.clone();
// Displaying the original TreeMap object
System.out.println("Original TreeMap: " + originalTreeMap);
// Displaying the cloned TreeMap object
System.out.println("Cloned TreeMap: " + clonedTreeMap);
// Modifying the cloned TreeMap object
clonedTreeMap.put(6, "Six");
// Displaying the original TreeMap object after modifying the cloned TreeMap object
System.out.println("Original TreeMap after modifying the cloned TreeMap: " + originalTreeMap);
// Displaying the cloned TreeMap object after modifying it
System.out.println("Cloned TreeMap after modifying it: " + clonedTreeMap);
}
}
Output:
Original TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Cloned TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Original TreeMap after modifying the cloned TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Cloned TreeMap after modifying it: {1=One, 2=Two, 3=Three, 4=Four, 5=Five, 6=Six}
In the above example, we have created a TreeMap object originalTreeMap
and added some key-value pairs to it. Then, we have created a shallow copy of the originalTreeMap
object using the clone()
method and stored it in clonedTreeMap
. We have displayed both the original and cloned TreeMap objects. Then, we have modified the clonedTreeMap
object by adding a new key-value pair to it. Finally, we have displayed both the original and cloned TreeMap objects again to show that modifying the cloned TreeMap object does not affect the original TreeMap object.
Example 2: Using clone() method to create a shallow copy of a TreeMap object with custom objects as values
import java.util.TreeMap;
class Employee {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public class TreeMapCloneExample {
public static void main(String[] args) {
// Creating a TreeMap object with custom objects as values
TreeMap<Integer, Employee> originalTreeMap = new TreeMap<>();
originalTreeMap.put(1, new Employee(101, "John"));
originalTreeMap.put(2, new Employee(102, "Mary"));
originalTreeMap.put(3, new Employee(103, "Bob"));
// Creating a shallow copy of the original TreeMap object
TreeMap<Integer, Employee> clonedTreeMap = (TreeMap<Integer, Employee>) originalTreeMap.clone();
// Displaying the original TreeMap object
System.out.println("Original TreeMap: " + originalTreeMap);
// Displaying the cloned TreeMap object
System.out.println("Cloned TreeMap: " + clonedTreeMap);
// Modifying the cloned TreeMap object
clonedTreeMap.put(4, new Employee(104, "Alice"));
// Displaying the original TreeMap object after modifying the cloned TreeMap object
System.out.println("Original TreeMap after modifying the cloned TreeMap: " + originalTreeMap);
// Displaying the cloned TreeMap object after modifying it
System.out.println("Cloned TreeMap after modifying it: " + clonedTreeMap);
}
}
Output:
Original TreeMap: {1=Employee{id=101, name='John'}, 2=Employee{id=102, name='Mary'}, 3=Employee{id=103, name='Bob'}}
Cloned TreeMap: {1=Employee{id=101, name='John'}, 2=Employee{id=102, name='Mary'}, 3=Employee{id=103, name='Bob'}}
Original TreeMap after modifying the cloned TreeMap: {1=Employee{id=101, name='John'}, 2=Employee{id=102, name='Mary'}, 3=Employee{id=103, name='Bob'}}
Cloned TreeMap after modifying it: {1=Employee{id=101, name='John'}, 2=Employee{id=102, name='Mary'}, 3=Employee{id=103, name='Bob'}, 4=Employee{id=104, name='Alice'}}
In the above example, we have created a TreeMap object originalTreeMap
with custom objects of the Employee
class as values. Then, we have created a shallow copy of the originalTreeMap
object using the clone()
method and stored it in clonedTreeMap
. We have displayed both the original and cloned TreeMap objects. Then, we have modified the clonedTreeMap
object by adding a new key-value pair to it. Finally, we have displayed both the original and cloned TreeMap objects again to show that modifying the cloned TreeMap object does not affect the original TreeMap object.