Java TreeMap clone()方法

java.util.TreeMap.clone() 返回此TreeMap中instance.But的浅表副本(本身键和值不被复制)。

1 语法

public Object clone()

2 参数

3 返回值

返回此映射的浅表副本。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.TreeMap.clone()方法的例子
 */
import java.util.*;

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

        // creating tree map
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
        TreeMap<Integer, String> treemapclone = new TreeMap<Integer, String>();

        // populating tree map
        treemap.put(2, "two");
        treemap.put(1, "one");
        treemap.put(3, "three");
        treemap.put(6, "six");
        treemap.put(5, "five");

        // cloning tree map
        System.out.println("Cloning tree map");
        treemapclone = (TreeMap)treemap.clone();

        System.out.println("Original map: "+ treemap);
        System.out.println("Cloned map: "+ treemapclone);
    }
}

输出结果为:

Cloning tree map
Original map: {1=one, 2=two, 3=three, 5=five, 6=six}
Cloned map: {1=one, 2=two, 3=three, 5=five, 6=six}

热门文章

优秀文章