📜  Java中的 HashTable putIfAbsent() 方法及示例

📅  最后修改于: 2022-05-13 01:55:33.710000             🧑  作者: Mango

Java中的 HashTable putIfAbsent() 方法及示例

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

句法:

public V putIfAbsent(K key, V value)

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

  • key :如果 key 没有与任何值关联,则指定将指定 value 映射到的 key。
  • value :指定要映射到指定键的值。

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

异常:此方法抛出:

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

下面的程序说明了 putIfAbsent(Key, value) 方法:

方案一:

// 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
            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
            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: Java/util/Hashtable.html#putIfAbsent-KV-