TreeMap clone() in Java

clone(): This method is available in java.util.TreeMap class of Java.

Syntax:

Object java.util.TreeMap.clone()

This method returns a shallow copy of this TreeMap instance. (The keys and values themselves are not cloned.)

Parameters: NA

Returns: a shallow copy of this map.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

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

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        System.out.println(treeMap.clone());
    }
}

Output:

{2=Hello, 6=C++, 11=Java, 23=Program, 25=Program}


No comments:

Post a Comment