ConcurrentHashMap 類的 getOrDefault() 方法返回指定鍵映射到的值,如果此映射不包含鍵的映射,則返回給定的默認值。
用法
public V getOrDefault(Object key, V defaultValue)
參數
key - 要返回其關聯值的鍵
defaultValue - 如果此映射不包含給定鍵的映射,則返回的值
返回
鍵的映射(如果存在);否則默認值
拋出
NullPointerException
例子1
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapgetOrDefaultExample1 {
public static void main(String[] args) throws InterruptedException {
int id= 8888;
ConcurrentHashMap<Integer, String> conmap = initialize();
System.out.println(" Id is:" + id + " "
+ conmap.getOrDefault(id , "JavaTpoint"));
}
private static ConcurrentHashMap<Integer, String> initialize() {
ConcurrentHashMap<Integer, String> Objmap = new ConcurrentHashMap<>();
Objmap.put(85, "Java");
Objmap.put(80, "php");
return Objmap;
}
}
輸出:
Id is:8888 JavaTpoint
相關用法
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap newKeySet()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap forEach()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap equals()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap mappingCount()用法及代碼示例
- Java ConcurrentHashMap merge()用法及代碼示例
- Java ConcurrentHashMap hashcode()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap computeIfPresent()用法及代碼示例
- Java ConcurrentHashMap toString()用法及代碼示例
- Java ConcurrentHashMap replace()用法及代碼示例
- Java ConcurrentHashMap clear()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ConcurrentHashMap getOrDefault() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。