當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。