当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ConcurrentHashMap put()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 ConcurrentHashMap put() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。