Java HashMap clone() 方法



描述

Java HashMap clone() 方法用於返回此 HashMap 例項的淺複製。

宣告

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

public Object clone()

引數

返回值

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

異常

清除 Integer,Integer 對的 HashMap 示例

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

package com.tutorialspoint;

import java.util.HashMap;

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

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

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

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

輸出

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

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

清除 Integer,String 對的 HashMap 示例

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

package com.tutorialspoint;

import java.util.HashMap;

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

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

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

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

輸出

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

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

清除 Integer,Student 對的 HashMap 示例

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

package com.tutorialspoint;

import java.util.HashMap;

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

      // 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 = (HashMap)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 ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
Cloned 2nd Map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
java_util_hashmap.htm
廣告
© . All rights reserved.