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-
相關用法
- Java HashMap putIfAbsent(key, value)用法及代碼示例
- Java Hashtable computeIfAbsent()用法及代碼示例
- Java HashTable compute()用法及代碼示例
- Java HashTable forEach()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java Hashtable contains()用法及代碼示例
- Java Hashtable get()用法及代碼示例
- Java Hashtable put()用法及代碼示例
- Java Hashtable elements()用法及代碼示例
- Java Hashtable toString()用法及代碼示例
- Java Hashtable remove()用法及代碼示例
- Java Hashtable clear()用法及代碼示例
- Java Hashtable keys()用法及代碼示例
- Java Hashtable isEmpty()用法及代碼示例
- Java Hashtable clone()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 HashTable putIfAbsent() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。