Java中的ConcurrentHashMap putAll()方法

这个 JAVAutil。同时发生的ConcurrentHashMap。putAll() 是Java中用于复制操作的内置函数。该方法将所有元素(即映射)从一个ConcurrentHashMap复制到另一个。

null

语法:

new_conn_hash_map.putAll(conn_hash_map)

参数: 该函数接受ConcurrentHashMap 康涅狄格地图 作为其唯一参数,并复制其所有映射 地图

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

例外情况: 函数抛出 空指针异常 当指定的参数为空时。

下面的程序演示了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()

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享