java.util.concurrent.ConcurrentHashMap.putAll()是Java中的內置函數,用於複製操作。該方法將所有元素(即映射)從一個ConcurrentHashMap複製到另一個。
用法:
new_conn_hash_map.putAll(conn_hash_map)
參數:該函數接受ConcurrentHashMap conn_hash_map作為其唯一參數,並使用此映射複製其所有映射。
返回值:該方法不返回任何值。
異常:當指定的參數為null時,該函數將引發NullPointerException。
以下示例程序旨在說明ConcurrentHashMap.putAll()方法:
示例1:該程序涉及將字符串值映射到整數鍵。
// Java Program Demonstrate putAll()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class ConcurrentHashMapDemo {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String> chm =
new ConcurrentHashMap<Integer, String>();
chm.put(100, "Geeks");
chm.put(101, "for");
chm.put(102, "Geeks");
chm.put(103, "Gfg");
chm.put(104, "GFG");
// Displaying the existing HashMap
System.out.println("Initial Mappings are: "
+ chm);
ConcurrentHashMap<Integer, String> new_chm =
new ConcurrentHashMap<Integer, String>();
new_chm.putAll(chm);
// Displaying the new map
System.out.println("New mappings are: "
+ new_chm);
}
}
輸出:
Initial Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG} New mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
示例2:該程序涉及將整數值映射到字符串鍵。
// Java Program Demonstrate putAll()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class ConcurrentHashMapDemo {
public static void main(String[] args)
{
ConcurrentHashMap<String, Integer> chm =
new ConcurrentHashMap<String, Integer>();
chm.put("Gfg", 100);
chm.put("GFG", 102);
chm.put("GfG", 18);
chm.put("gfg", 15);
chm.put("gfG", 55);
// Displaying the existing HashMap
System.out.println("Initial Mappings are: "
+ chm);
ConcurrentHashMap<String, Integer> new_chm =
new ConcurrentHashMap<String, Integer>();
// Copying the contents
new_chm.putAll(chm);
// Inserting a new mapping
new_chm.put("gFg", 22);
// Displaying the new map
System.out.println("New mappings are: "
+ new_chm);
}
}
輸出:
Initial Mappings are: {Gfg=100, GFG=102, GfG=18, gfg=15, gfG=55} New mappings are: {Gfg=100, GFG=102, GfG=18, gfg=15, gfG=55, gFg=22}
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#putAll()
相關用法
- Java EnumMap putAll(map)用法及代碼示例
- Java TreeMap putAll()用法及代碼示例
- Java IdentityHashMap putAll()用法及代碼示例
- Java WeakHashMap putall()用法及代碼示例
- Java HashMap putAll()用法及代碼示例
- Java Map putAll()用法及代碼示例
- Java SortedMap putAll()用法及代碼示例
- Java SimpleBindings putAll()用法及代碼示例
- Java AbstractMap putAll()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentHashMap putAll() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。