返回 Java 中 IdentityHashMap 的淺複製


返回淺複製意味著將一個 IdentityHashMap 中的元素複製到另一個 IdentityHashMap 中。為此,使用 clone() 方法。

以下是返回 IdentityHashMap 淺複製的一個示例

示例

 線上演示

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      IdentityHashMap<String, Integer> m = newIdentityHashMap<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()); 0System.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

更新時間:30-7-2019

77 瀏覽量

開啟你的事業

完成課程以獲得認證

立即開始
廣告