前言
在jdk1.8中Map接口新增了一个computeIfAbsent
方法,这是Map接口中的默认实现
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v;
if ((v = get(key)) == null) {
V newValue;
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue;
}
}
return v;
}
该方法是首先判断缓存Map中是否存在指定的key的值,如果不存在,会调用mappingFunction(key)
计算key的value,然后将key = value 放入缓存Map中。但如果mappingFunction(key)
计算出来的value为null或抛出异常,则不会记录缓存。
代码实例
例1
/**
* 如果key对应的value值为null,则在map中放入该key和设置相应的value
*/
public class Test {
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap<>();
/**
* 方法1:使用方法引用
*/
map.computeIfAbsent(1,Test::compute);
/**
* 方法2:使用匿名内部类
*/
map.computeIfAbsent(1, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer;
}
});
/**
* 方法3:使用lambda
*/
map.computeIfAbsent(1,key -> key);
System.out.println(map.get(1));
}
static public Integer compute(Integer integer){
return integer;
}
}
例2
/**
* 统计List出现相同字符串的个数
*/
public class Test {
public static void main(String[] args) {
Map<String,AtomicInteger> map = new HashMap<>();
List<String> list = Arrays.asList(new String[]{"1","2","2","3","3","4","4","4"});
list.forEach(str->map.computeIfAbsent(str,k->new AtomicInteger()).incrementAndGet());
System.out.println(map);
}
}
/**
* {1=1, 2=2, 3=2, 4=3}
*/
这里用了两次lambda,和下面方法是一样
/**
* 统计List出现相同字符串的个数
*/
public class Test {
public static void main(String[] args) {
Map<String,AtomicInteger> map = new HashMap<>();
List<String> list = Arrays.asList(new String[]{"1","2","2","3","3","4","4","4"});
list.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
//寻找map中key对应的value,如果map不存在指定key的值则new 一个AtomicInteger对象并加入缓存map中,如果存在则取到对应的AtomicInteger对象并加一
map.computeIfAbsent(s, new Function<String, AtomicInteger>() {
@Override
public AtomicInteger apply(String s) {
return new AtomicInteger();
}
}).incrementAndGet();
}
});
System.out.println(map);
}
}
/**
* {1=1, 2=2, 3=2, 4=3}
*/
总结
computeIfAbsent在一些实际开发场景中,能让我们代码看去更加简洁,代码质量看去也更高。