Java中的ConcurrentHashMap putAll()方法

Java中的ConcurrentHashMap putAll()方法

Java中的ConcurrentHashMap类中的putAll()方法用于将指定映射的所有键值映射复制到当前映射中。它具有以下签名:

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

其中:

m是要将其键值映射复制到当前映射中的映射。

putAll()方法可以在并发环境中工作,这意味着可以从多个线程调用它,而不会导致任何数据竞争或同步问题。

当进行putAll()操作时,该方法首先获取当前映射的所有段上的锁定,然后将指定映射的所有键值映射复制到当前映射中。

以下是使用putAll()方法的示例:

import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.Map;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        ConcurrentHashMap<String, Integer> map1 = new ConcurrentHashMap<>();
        map1.put("Alice", 25);
        map1.put("Bob", 30);

        Map<String, Integer> map2 = new HashMap<>();
        map2.put("Charlie", 35);
        map2.put("Dave", 40);

        map1.putAll(map2);

        System.out.println("Combined map: " + map1);
    }
}

输出:

Combined map: {Bob=30, Alice=25, Charlie=35, Dave=40}

java.util.concurrent.ConcurrentHashMap.putAll() 是Java中的内置功能,用于复制操作。该方法将一个ConcurrentHashMap的所有元素即映射,复制到另一个ConcurrentHashMap中。

语法:

new_conn_hash_map.putAll _(conn_hash_map)_

参数: 函数将ConcurrentHashMap conn_hash_map 作为其唯一参数,复制其所有映射与此映射。

返回值: 该方法不返回任何值。

异常: 如果指定的参数为空,则函数会抛出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程序演示ConcurrentHashMap的putAll()方法
 
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);

        //显示现有的HashMap
        System.out.println("Initial Mappings are: " + chm);

        ConcurrentHashMap<String, Integer> new_chm
            = new ConcurrentHashMap<String, Integer>();

        //复制内容
        new_chm.putAll(chm);

        //插入新映射
        new_chm.put("gFg", 22);
        //显示新地图
        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}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程