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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。