Java中的ConcurrentHashmap類的put()方法用於將映射插入此映射中。它將參數作為(鍵,值)對。如果現有鍵傳遞了值,則此方法將更新該鍵的值。否則,如果通過了新的配對,則該配對將作為一個整體插入。
用法:
public V put(K key, V value)
參數:此方法接受兩個強製性參數:
- key:–與指定值關聯的鍵
- value:–與指定鍵關聯的值
返回值:此方法返回與key關聯的先前值;如果沒有key映射,則返回null。
異常:如果指定的鍵或值為null,則此方法引發NullPointerException
下麵是說明put()方法的示例:
示例1:當鍵值傳遞時是新的。
// Java program to demonstrate
// put() method
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map<String, String>
my_cmmap = new ConcurrentHashMap<String, String>();
// Adding elements to the map
// using put() method
my_cmmap.put("1", "1");
my_cmmap.put("2", "1");
my_cmmap.put("3", "1");
my_cmmap.put("4", "1");
my_cmmap.put("5", "1");
my_cmmap.put("6", "1");
// Printing the map
System.out.print(my_cmmap);
}
}
輸出:
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
示例2:當現有鍵值被傳遞時。
// Java program to demonstrate
// put() method
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map<String, String>
my_map = new ConcurrentHashMap<String, String>();
// Variable to get the returned value by put()
String returnedValue;
// Adding elements to the map
// using put() method
// and printing the returned value each time
returnedValue = my_map.put("Geek", "100");
System.out.println("Map: " + my_map);
System.out.println("Returned Value: " + returnedValue);
System.out.println();
returnedValue = my_map.put("Geek", "200");
System.out.println("Map: " + my_map);
System.out.println("Returned Value: " + returnedValue);
System.out.println();
returnedValue = my_map.put("Geek", "300");
System.out.println("Map: " + my_map);
System.out.println("Returned Value: " + returnedValue);
System.out.println();
returnedValue = my_map.put("Geek", "400");
System.out.println("Map: " + my_map);
System.out.println("Returned Value: " + returnedValue);
System.out.println();
returnedValue = my_map.put("Geek", "500");
System.out.println("Map: " + my_map);
System.out.println("Returned Value: " + returnedValue);
System.out.println();
System.out.print(my_map);
}
}
輸出:
Map: {Geek=100} Returned Value: null Map: {Geek=200} Returned Value: 100 Map: {Geek=300} Returned Value: 200 Map: {Geek=400} Returned Value: 300 Map: {Geek=500} Returned Value: 400 {Geek=500}
示例3:演示NullPointerException
// Java program to demonstrate
// put() method
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map<String, String>
my_cmmap = new ConcurrentHashMap<String, String>();
// Adding elements to the map
// using put() method
my_cmmap.put("1", "1");
my_cmmap.put("2", "1");
my_cmmap.put("3", "1");
my_cmmap.put("4", "1");
my_cmmap.put("5", "1");
my_cmmap.put("6", "1");
// Printing the map
System.out.println(my_cmmap + "\n");
// When null key is passed
try {
System.out.println("When (null, \"10\") "
+ "is passed as parameter:");
my_cmmap.put(null, "10");
}
catch (Exception e) {
System.out.println("Exception: " + e + "\n");
}
// When null value is passed
try {
System.out.println("When (\"10\", null) "
+ "is passed as parameter:");
my_cmmap.put("10", null);
}
catch (Exception e) {
System.out.println("Exception: " + e + "\n");
}
}
}
輸出:
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1} When (null, "10") is passed as parameter: Exception: java.lang.NullPointerException When ("10", null) is passed as parameter: Exception: java.lang.NullPointerException
相關用法
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap containsValue()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap remove()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
- Java ConcurrentHashMap keySet()用法及代碼示例
- Java ConcurrentHashMap entrySet()用法及代碼示例
- Java ConcurrentHashMap keys()用法及代碼示例
- Java ConcurrentHashMap values()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 ConcurrentHashMap put() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。