克隆 Java 中的 IdentityHashMap
要在 Java 中克隆 IdentityHashMap,可以使用 clone() 方法。
创建一个 IdentityHashMap 并向其中添加元素 −
IdentityHashMap<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);现在获取 IdentityHashMap 的大小 −
m.size()
以下是有关如何在 Java 中克隆 IdentityHashMap 的示例 −
示例
import java.util.*;
public class Demo {
public static void main(String[] args) {
IdentityHashMap<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);
System.out.println("IdentityHashMap elements
"+ m);
System.out.println("Size = " + m.size());
System.out.println("
Cloned Map = " + m.clone());
System.out.println("Size = " + m.size());
}
}输出
IdentityHashMap elements
{2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Size = 10
Cloned Map = {2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Size = 10
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP