Java
分享一个实用的 Java 8 开发技能,那就是 Map 接口中增加的 compute 方法,给 Map 集合计算更新用的。
compute简介
如下所示,Java 8 在 Map 和 ConcurrentMap 接口中都增加了 3 个 compute 方法,说明也是支持多线程并发安全操作的。

这三个方法的区别:
compute:计算并更新值computeIfAbsent:Value不存在时才计算-
compute有啥用?
话说这有什么卵用?
先看看没用 Java 8 的一个小示例:private static void preJava8() {List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");Map<String, Integer> map = new HashMap<>();for(String animal : animals){Integer count = map.get(animal);map.put(animal, count == null ? 1 : ++count);}System.out.println(map);}
输出:
{cat=2, fish=1, dog=3}
这是一个统计一个列表中每个动物的数量,代码再怎么精简都需要一步
get操作,判断集合中是否有元素再确定是初始化:1,还是需要 +1。
很多时候,这个get操作显然是毫无必要的,所以 Java 8 提供了 3 个compute方法,来看看怎么用吧!
Java 8 compute 实现方式:private static void inJava8() {List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");Map<String, Integer> map = new HashMap<>();for(String animal : animals){map.compute(animal, (k, v) -> v == null ? 1 : ++v);}System.out.println(map);}
使用
compute方法一行搞定,省去了需要使用get取值再判断的冗余操作,直接就可以获取元素值并计算更新,是不是很方便呢?compute源码分析这还是一个默认方法,为什么是默认方法,也是为了不改动其所有实现类。
default V compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction) {// 函数式接口不能为空Objects.requireNonNull(remappingFunction);// 获取旧值V oldValue = get(key);// 获取计算的新值V newValue = remappingFunction.apply(key, oldValue);if (newValue == null) { // 新值为空// delete mappingif (oldValue != null || containsKey(key)) { // 旧值存在时// 移除该键值remove(key);return null;} else {// nothing to do. Leave things as they were.return null;}} else { // 新值不为空// 添加或者覆盖旧值put(key, newValue);return newValue;}}
实现逻辑其实也很简单,其实就是结合了 Java 8 的函数式编程让代码变得更简单了。
