你只会用 map.put?试试 Java 8 compute ,操作 Map 更轻松!
1. Map 接口中的 compute 方法
1.1 compute简介接口中 compute,给 Map 集合计算更新用的。
如下所示,Java 8 在 Map 和 ConcurrentMap 接口中都增加了 3 个 compute 方法,说明也是支持多线程并发安全操作的。
1.2 这三个方法的区别
1. compute:计算并更新值
2. computeIfAbsent:Value不存在时才计算
3. computeIfPresent:Value存在时才计算
2. 示例代码
2.1 没有用 Java 8 的示例
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
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 = " + map);
}
}
输出: map = {cat=2, fish=1, dog=3}
这是一个统计一个列表中每个动物的数量,代码再怎么精简都需要一步 get 操作,判断集合中是否有元素再确定是初始化:1,还是需要 +1。
很多时候,这个 get 操作显然是毫无必要的,所以 Java 8 提供了 3 个 compute 方法,来看看怎么用吧!
2.2 Java 8 compute 实现方式
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
List<String> animalList = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
Map<String, Integer> maps = new HashMap<>();
for (String animal : animalList) {
maps.compute(animal, (k, v) -> v == null ? 1 : ++v);
}
System.out.println("maps = " + maps);
}
}
输出: maps = {fish=1, cat=2, dog=3}
使用 compute 方法一行搞定,省去了需要使用 get
取值再判断的冗余操作,直接就可以获取元素值并计算更新,是不是很方便呢?
2.3 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 mapping
// 旧值存在时
if (oldValue != null || containsKey(key)) {
// something to remove
// 移除该键值
remove(key);
return null;
} else {
// nothing to do. Leave things as they were.
return null;
}
} else { // 新值不为空
// add or replace old mapping
// 添加或者覆盖旧值
put(key, newValue);
return newValue;
}
}
实现逻辑其实也很简单,其实就是结合了 Java 8 的函数式编程让代码变得更简单了,Java 也越来越聪明了。
另外两个方法就不演示了,在特定的场合肯定也肯定特别有用,大家知道就好,需要的时候要知道拿来用。
2.4 完整代码及测试结果
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
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 = " + map);
List<String> animalList = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
Map<String, Integer> maps = new HashMap<>();
for (String animal : animalList) {
maps.compute(animal, (k, v) -> v == null ? 1 : ++v);
}
System.out.println("maps = " + maps);
}
}