In Java, clone() returns a shallow copy of a TreeMap: a new tree with the same keys, the same values, and the same comparator. Adding or removing mappings in the copy does not affect the original, and vice versa.
"Shallow" means the key and value objects are not copied: both maps point to the same instances. That is harmless for immutable types such as String and Integer, but a mutable value like a List is shared. The copy is built in O(n) time from the already sorted entries.
An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.
Copy constructor; keeps the comparator when map is a SortedMap.
new TreeMap<>(); copy.putAll(map)
Copy into a naturally ordered map.
The cast from Object to TreeMap<K, V> is unchecked, so the compiler warns unless you add @SuppressWarnings("unchecked"). The copy constructor new TreeMap<>(sortedMap) needs no cast and also keeps the comparator, which is why many developers prefer it.
Library declarations
Java
// java.util.TreeMap<K, V>
public Object clone()
// Overrides AbstractMap.clone()
Parameters
Parameter
Description
(none)
clone takes no parameters.
Return Value
Return Value
Description
Object
A new TreeMap with the same mappings and comparator; cast it to TreeMap<K, V>.
A cast with a warning suppressed.clone() is declared to return Object, so the result is cast to TreeMap<String, Integer> and the unchecked warning is silenced on that one declaration.
Own tree, own entries. Adding soup and removing tea change only draft; the copy has its own nodes.
The original survives.prices still holds cake and tea, so the draft can be thrown away or adopted later.
Example 2 — A Cloned Map Shares Its List Values
Each team maps to an ArrayList; the clone holds the same list objects, not copies.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
public class TreeMapCloneShared {
public static void main(String[] args) {
TreeMap<String, List<String>> teams = new TreeMap<>();
teams.put("blue", new ArrayList<>(Arrays.asList("Ana")));
@SuppressWarnings("unchecked")
TreeMap<String, List<String>> copy = (TreeMap<String, List<String>>) teams.clone();
copy.get("blue").add("Ben");
copy.put("red", new ArrayList<>());
System.out.println("original: " + teams);
System.out.println("copy: " + copy);
System.out.println("same list object: " + (teams.get("blue") == copy.get("blue")));
}
}
Output
original: {blue=[Ana, Ben]}
copy: {blue=[Ana, Ben], red=[]}
same list object: true
How It Works
Mutating a shared value.copy.get("blue").add("Ben") changes the one list both maps point to, so Ben appears in the original too.
New keys stay private. The "red" team was put only into copy, and teams never sees it.
Identity proves it.== on the two "blue" lists is true: the clone copied the reference, not the list.
Example 3 — clone() Keeps a Reverse Comparator
The clone keeps the ranking order; copying with putAll into a plain TreeMap does not.