1. Overview
1.概述
In this tutorial, we’ll discuss the new default method, computeIfAbsent, of the Map interface introduced in Java 8.
在本教程中,我们将讨论Java 8中引入的Map接口的新默认方法computeIfAbsent>。
Specifically, we’ll look at its signature, usage, and how it handles different cases.
具体来说,我们将看看它的签名、用法,以及它如何处理不同的情况。
2. Map.computeIfAbsent Method
2.Map.computeIfAbsent方法
Let’s start by looking at the signature of computeIfAbsent:
让我们先看一下computeIfAbsent的签名。
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
The computeIfAbsent method takes two parameters. The first parameter is the key, and the second parameter is the mappingFunction. It’s important to know that the mapping function is only called if the mapping isn’t present.
computeIfAbsent方法需要两个参数。第一个参数是key,和第二个参数是mappingFunction。重要的是要知道,只有在映射不存在的情况下才会调用映射函数。
2.1. Key Related to a Non-Null Value
2.1.与非空值有关的键
First, it checks if the key is present in the map. If the key is present, and a non-null value is related to the key, then it returns that value:
首先,它检查key是否存在于地图中。如果键存在,并且有一个非空值与该键相关,那么它就返回该值。
Map<String, Integer> stringLength = new HashMap<>();
stringLength.put("John", 5);
assertEquals((long)stringLength.computeIfAbsent("John", s -> s.length()), 5);
As we can see, the key “John” has a non-null mapping present, and it returns the value 5. If our mapping function was used, we’d expect the function to return the length of 4.
我们可以看到,键 “John”有一个非空的映射存在,并且它返回的值是5。如果我们的映射函数被使用,我们会期望该函数返回长度为4。
2.2. Using the Mapping Function to Compute the Value
2.2.使用映射函数来计算数值
Furthermore, if the key isn’t present in the map, or the null value is related to the key, then it attempts to compute the value using the given mappingFunction. It also enters the calculated value into the map unless the calculated value is null.
此外,如果key在地图中不存在,或者空值与key有关,那么它试图使用给定的mappingFunction来计算该值。它还会将计算出的值输入到地图中,除非计算出的值为空。
Let’s take a look at the use of the mappingFunction in the computeIfAbsent method:
让我们来看看 mappingFunction在computeIfAbsent方法中的使用。
Map<String, Integer> stringLength = new HashMap<>();
assertEquals((long)stringLength.computeIfAbsent("John", s -> s.length()), 4);
assertEquals((long)stringLength.get("John"), 4);
Since the key “John” isn’t present, it computes the value by passing the key as a parameter to the mappingFunction.
由于key “John”不存在,它通过将key作为参数传递给mappingFunction来计算值。
2.3. Mapping Function Returns null
2.3.映射函数返回null
Also, if the mappingFunction returns null, the map records no mapping:
另外,如果mappingFunction返回null,则地图没有记录映射。
Map<String, Integer> stringLength = new HashMap<>();
assertEquals(stringLength.computeIfAbsent("John", s -> null), null);
assertNull(stringLength.get("John"));
2.4. Mapping Function Throws an Exception
2.4.映射函数抛出一个异常
Finally, if the mappingFunction throws an unchecked exception, then the exception is re-thrown, and the map records no mapping:
最后,如果mappingFunction抛出一个未检查的异常,那么该异常会被重新抛出,并且地图没有记录映射。
@Test(expected = RuntimeException.class)
public void whenMappingFunctionThrowsException_thenExceptionIsRethrown() {
Map<String, Integer> stringLength = new HashMap<>();
stringLength.computeIfAbsent("John", s -> { throw new RuntimeException(); });
}
We can see that the mappingFunction throws a RuntimeException, which propagates back to the computeIfAbsent method.
我们可以看到,mappingFunction抛出了一个RuntimeException,它传播回了computeIfAbsent方法。
3. Conclusion
3.总结
In this brief article, we focused on the computeIfAbsent method, its signature, and its usage. Finally, we learned how it handles different cases.
在这篇简短的文章中,我们重点介绍了computeIfAbsent方法,它的签名和用法。最后,我们了解了它如何处理不同的情况。
As always, all of these code samples are available over on GitHub.
一如既往,所有这些代码样本都可以在GitHub上找到over。