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


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


java.util.concurrent.ConcurrentHashMap.putIfAbsent()是Java中的内置函数,它接受键和值作为参数,如果指定的键未映射为任何值,则将其映射。

用法:

chm.putIfAbsent(key_elem, val_elem)

参数:该函数接受以下两个参数:


  • key_elem:如果key_elem没有与任何值关联,则此参数指定将指定val_elem映射到的键。
  • val_elem:此参数指定要映射到指定的key_elem的值。

返回值:该函数返回映射到键的现有值,如果以前没有映射到键的值,则返回null。

异常:当指定的参数为null时,该函数将引发NullPointerException。

以下示例程序旨在说明ConcurrentHashMap.putIfAbsent()方法:

示例1:现有键作为参数传递给函数。

// Java Program Demonstrate putIfAbsent() 
// 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 HashMap 
        System.out.println("Initial Mappings are: "
                           + chm); 
  
        // Inserting non-existing key along with value 
        String returned_value = (String)chm.putIfAbsent(108, "All"); 
  
        // Verifying the returned value 
        System.out.println("Returned value is: "
                           + returned_value); 
  
        // Displayin the new map 
        System.out.println("New mappings are: "
                           + chm); 
    } 
}
输出:
Initial Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
Returned value is: null
New mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG, 108=All}

示例2:不存在的键作为参数传递给函数。

// Java Program Demonstrate putIfAbsent() 
// 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 HashMap 
        System.out.println("Initial Mappings are: "
                           + chm); 
  
        // Inserting existing key along with value 
        String returned_value = (String)chm.putIfAbsent(100, "All"); 
  
        // Verifying the returned value 
        System.out.println("Returned value is: "
                           + returned_value); 
  
        // Displayin the new map 
        System.out.println("New mappings are: "
                           + chm); 
    } 
}
输出:
Initial Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
Returned value is: Geeks
New mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent()



相关用法


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