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


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


Hashtable类的putIfAbsent(Key,value)方法,如果给定键不与值关联或映射为null,则该方法允许将值映射到给定键。如果HashMap中已经存在这样的键值集,则返回null值。

用法:

public V putIfAbsent(K key, V value)

参数:此方法接受两个参数:


  • key:指定键(如果键未与任何值关联)将指定值映射到的键。
  • value:指定要映射到指定键的值。

返回值:此方法返回映射到键的现有值,如果以前没有映射到键的值,则返回null。

异常:该方法抛出:

  • NullPointerException :当指定参数为null时。

以下示例程序旨在说明putIfAbsent(Key,value)方法:

示例1:

// Java program to demonstrate 
// putIfAbsent(key, value) method. 
  
import java.util.*; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        // create a table and add some values 
        Map<String, Integer> 
            table = new Hashtable<>(); 
  
        table.put("Pen", 10); 
        table.put("Book", 500); 
        table.put("Clothes", 400); 
        table.put("Mobile", 5000); 
  
        // print map details 
        System.out.println("hashTable: "
                           + table.toString()); 
  
        // Inserting non-existing key with value 
        // using putIfAbsent method 
        String retValue 
            = String.valueOf(table 
                                 .putIfAbsent("Booklet", 2500)); 
  
        // Print the returned value 
        System.out.println("Returned value "
                           + "for Key 'Booklet' is: "
                           + retValue); 
  
        // print new mapping 
        System.out.println("hashTable: "
                           + table); 
  
        // Inserting existing key with value 
        // using putIfAbsent method 
        retValue 
            = String.valueOf(table 
                                 .putIfAbsent("Book", 4500)); 
  
        // Print the returned value 
        System.out.println("Returned value"
                           + " for key 'Book' is: "
                           + retValue); 
  
        // print new mapping 
        System.out.println("hashTable: "
                           + table); 
    } 
}
输出:
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400}
Returned value for Key 'Booklet' is: null
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400, Booklet=2500}
Returned value for key 'Book' is: 500
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400, Booklet=2500}

示例2:显示NullPointerException

// Java program to demonstrate 
// putIfAbsent(key, value) method. 
  
import java.util.*; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        // create a table and add some values 
        Map<Integer, String> 
            table = new Hashtable<>(); 
  
        table.put(1, "100RS"); 
        table.put(2, "500RS"); 
        table.put(3, "1000RS"); 
  
        // print map details 
        System.out.println("hashTable: "
                           + table.toString()); 
  
        try { 
  
            table.putIfAbsent(null, "8"); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
hashTable: {3=1000RS, 2=500RS, 1=100RS}
Exception: java.lang.NullPointerException

参考:https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html#putIfAbsent-K-V-



相关用法


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