Java HashMap clone()方法

java.util.HashMap.clone() 用于返回此HashMap实例的浅表副本:键和值本身不会被克隆。

1 语法

public Object clone()

2 参数

3 返回值

返回此映射的浅表副本。

4 示例 

package com.yiidian;

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

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

        // create two hash maps
        HashMap newmap1 = new HashMap();
        HashMap newmap2 = new HashMap();

        // populate 1st map
        newmap1.put(1, "tutorials");
        newmap1.put(2, "point");
        newmap1.put(3, "is best");

        // clone 1st map
        newmap2 = (HashMap)newmap1.clone();

        System.out.println("1st Map: " + newmap1);
        System.out.println("Cloned 2nd Map: " + newmap2);
    }
}

输出结果为:

1st Map: {1=tutorials, 2=point, 3=is best}
Cloned 2nd Map: {1=tutorials, 2=point, 3=is best}

热门文章

优秀文章