Java IdentityHashMap clone() 方法



描述

Java IdentityHashMap clone() 方法用於返回此身份雜湊對映的淺複製。

宣告

以下是 java.util.IdentityHashMap.clone() 方法的宣告。

public Object clone()

引數

返回值

方法呼叫返回此對映的淺複製。

異常

獲取整數、整數對 IdentityHashMap 的克隆示例

以下示例演示了使用 Java IdentityHashMap clone() 方法獲取對映的淺複製。我們建立了兩個整數、整數對的對映物件。然後向一個對映中添加了一些條目,使用 clone() 方法填充另一個對映,然後列印這兩個對映。

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      IdentityHashMap<Integer, Integer> newmap1 = new IdentityHashMap<>();
      IdentityHashMap<Integer, Integer> newmap2 = new IdentityHashMap<>();

      // populate 1st map
      newmap1.put(1, 1);
      newmap1.put(2, 1);
      newmap1.put(3, 1); 

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

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

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

1st Map: {2=1, 3=1, 1=1}
Cloned 2nd Map: {2=1, 3=1, 1=1}

獲取整數、字串對 IdentityHashMap 的克隆示例

以下示例演示了使用 Java IdentityHashMap clone() 方法獲取對映的淺複製。我們建立了兩個整數、字串對的對映物件。然後向一個對映中添加了一些條目,使用 clone() 方法填充另一個對映,然後列印這兩個對映。

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      IdentityHashMap<Integer, String> newmap1 = new IdentityHashMap<>();
      IdentityHashMap<Integer, String> newmap2 = new IdentityHashMap<>();

      // populate 1st map
      newmap1.put(1, "A");
      newmap1.put(2, "B");
      newmap1.put(3, "C"); 

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

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

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

1st Map: {2=B, 3=C, 1=A}
Cloned 2nd Map: {2=B, 3=C, 1=A}

獲取整數、物件對 IdentityHashMap 的克隆示例

以下示例演示了使用 Java IdentityHashMap clone() 方法獲取對映的淺複製。我們建立了兩個整數、學生對的對映物件。然後向一個對映中添加了一些條目,使用 clone() 方法填充另一個對映,然後列印這兩個對映。

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      IdentityHashMap<Integer, Student> newmap1 = new IdentityHashMap<>();
      IdentityHashMap<Integer, Student> newmap2 = new IdentityHashMap<>();

      // populate hash map
      newmap1.put(1, new Student(1, "Julie"));
      newmap1.put(2, new Student(2, "Robert"));
      newmap1.put(3, new Student(3, "Adam"));

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

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

1st Map: {1=[ 1, Julie ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
Cloned 2nd Map: {1=[ 1, Julie ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
java_util_identityhashmap.htm
廣告
© . All rights reserved.