如何在 Java 中克隆 Map
java.util.HashMap 类是基于哈希表的 Map 接口的实现。若要克隆 Java 中的 Map,请使用 clone() 方法。
举例
我们看一个克隆 Map 的例子 −
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// create two hash maps
HashMap newmap1 = new HashMap();
HashMap newmap2 = new HashMap();
// populate 1st map
newmap1.put(1, "This");
newmap1.put(2, "is");
newmap1.put(3, "it!");
// clone 1st map
newmap2 = (HashMap)newmap1.clone();
System.out.println("1st Map: " + newmap1);
System.out.println("Cloned Map: " + newmap2);
}
}输出
1st Map: {1=This, 2=is, 3=it!}
Cloned Map: {1=This, 2=is, 3=it!}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP