Java IdentityHashMap putAll() 方法



描述

Java IdentityHashMap putAll(Map<? extends K,? extends V> t) 方法用於將指定對映中的所有映射覆制到此對映。

宣告

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

public void putAll(Map<? extends K,? extends V> t)

引數

t − 這是要儲存在此對映中的對映。

返回值

方法呼叫返回與鍵關聯的先前值,如果鍵沒有對映,則返回 null。

異常

NullPointerException − 如果指定的對映為 null,則丟擲此異常。

向 Integer,Integer 對的 IdentityHashMap 新增多個條目示例

以下示例演示瞭如何使用 Java IdentityHashMap putAll() 方法將一些值放入 Map 中。我們建立了一個 Integer,Integer 對的 Map 物件。然後使用 put() 方法添加了一些條目,然後列印對映。使用 putAll() 方法填充一個新的對映,然後列印。

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create identity map
      IdentityHashMap<Integer,Integer> newmap = new IdentityHashMap<>();
	  IdentityHashMap<Integer,Integer> newmap2 = new IdentityHashMap<>();
 
      // populate identity map
      newmap.put(1, 1);
      newmap.put(2, 2);
      newmap.put(3, 3); 

      System.out.println("Map elements: " + newmap);
      newmap2.putAll(newmap);
      System.out.println("Map elements: " + newmap2); 
   }    
}

輸出

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

Map elements: {2=2, 3=3, 1=1}
Map elements: {2=2, 3=3, 1=1}

向 Integer,String 對的 IdentityHashMap 新增多個條目示例

以下示例演示瞭如何使用 Java IdentityHashMap putAll() 方法將一些值放入 Map 中。我們建立了一個 Integer,String 對的 Map 物件。然後使用 put() 方法添加了一些條目,然後列印對映。使用 putAll() 方法填充一個新的對映,然後列印。

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create identity map
      IdentityHashMap<Integer,String> newmap = new IdentityHashMap<>();
	  IdentityHashMap<Integer,String> newmap2 = new IdentityHashMap<>();
      // populate identity map
      newmap.put(1, "tutorials");
      newmap.put(2, "point");
      newmap.put(3, "is best"); 

      System.out.println("Map elements: " + newmap);
      newmap2.putAll(newmap);
      System.out.println("Map elements: " + newmap2);
   }    
}

輸出

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

Map elements: {2=point, 3=is best, 1=tutorials}
Map elements: {2=point, 3=is best, 1=tutorials}

向 Integer,Object 對的 IdentityHashMap 新增多個條目示例

以下示例演示瞭如何使用 Java IdentityHashMap putAll() 方法將一些值放入 Map 中。我們建立了一個 Integer,Student 對的 Map 物件。然後使用 put() 方法添加了一些條目,然後列印對映。使用 putAll() 方法填充一個新的對映,然後列印。

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create identity map
      IdentityHashMap<Integer,Student> newmap = new IdentityHashMap<>();
      IdentityHashMap<Integer,Student> newmap2 = new IdentityHashMap<>();
      // populate identity map
      newmap.put(1, new Student(1, "Julie"));
      newmap.put(2, new Student(2, "Robert"));
      newmap.put(3, new Student(3, "Adam"));

      System.out.println("Map elements: " + newmap);
      newmap2.putAll(newmap);
      System.out.println("Map elements: " + 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 + " ]";
   }
}

輸出

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

Map elements: {1=[ 1, Julie ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
Map elements: {1=[ 1, Julie ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
java_util_identityhashmap.htm
廣告