TreeMap Object clone() Method Example Program


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

Program

package com.candidjava;

import java.util.TreeMap;

/**
 * @author: Hari Haran
 * @discription: The clone() method is used to return a shallow copy of this
 *               TreeMap instance.But the keys and values themselves are not
 *               cloned.
 */

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

		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
		TreeMap<Integer, String> tmclone = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		System.out.println("Cloning tree map");
		tmclone = (TreeMap) tm.clone();

		System.out.println("Original map: " + tm);
		System.out.println("Cloned map: " + tmclone);
	}
}

Output

Cloning tree map
Original map: {1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal}
Cloned map: {1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal}

Explanation

public Object clone()
Returns a shallow copy of this TreeMap instance. (The keys and values themselves are not cloned.)
Overrides:
clone in class AbstractMap<K,V>
Returns:
a shallow copy of this map


Related Post

Comments


©candidjava.com