如何使用新的computeIfAbsent函数? [英] How do I use the new computeIfAbsent function?

查看:178
本文介绍了如何使用新的computeIfAbsent函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常想使用 Map.computeIfAbsent ,但自从本科以来的兰布斯已经太久了。

I very much want to use Map.computeIfAbsent but it has been too long since lambdas in undergrad.

直接从文档中直接显示:它给出了一个旧的方式来做一个例子:

Almost directly from the docs: it gives an example of the old way to do things:

Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
String key = "snoop";
if (whoLetDogsOut.get(key) == null) {
  Boolean isLetOut = tryToLetOut(key);
  if (isLetOut != null)
    map.putIfAbsent(key, isLetOut);
}

新的方式:

map.computeIfAbsent(key, k -> new Value(f(k)));

但是在他们的例子中,我觉得我不是得到它。如何转换代码以使用新的lambda表达方式?

But in their example, I think I'm not quite "getting it." How would I transform the code to use the new lambda way of expressing this?

推荐答案

假设您有以下代码: p>

Suppose you have the following code:

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Test {
    public static void main(String[] s) {
        Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
        whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
        whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
    }
    static boolean f(String s) {
        System.out.println("creating a value for \""+s+'"');
        return s.isEmpty();
    }
}

然后你会看到消息在第二次调用 computeIfAbsent 时,正好创建一个值为snoop的值,该键已经有一个值。 lambda表达式 k中的 k f(k)只是映射将传递给lambda的密钥的一个placeolder(参数),用于计算该值。所以在这个例子中,密钥被传递给函数调用。

Then you will see the message creating a value for "snoop" exactly once as on the second invocation of computeIfAbsent there is already a value for that key. The k in the lambda expression k -> f(k) is just a placeolder (parameter) for the key which the map will pass to your lambda for computing the value. So in the example the key is passed to the function invocation.

或者你可以写: whoLetDogsOut.computeIfAbsent(snoop,k - > ; k.isEmpty()); 在没有辅助方法的情况下实现相同的结果(但是您将看不到调试输出)。甚至更简单,因为它是对现有方法的简单委派,您可以写入: whoLetDogsOut.computeIfAbsent(snoop,String :: isEmpty); 此代理不需要要写入的任何参数。

Alternatively you could write: whoLetDogsOut.computeIfAbsent("snoop", k -> k.isEmpty()); to achieve the same result without a helper method (but you won’t see the debugging output then). And even simpler, as it is a simple delegation to an existing method you could write: whoLetDogsOut.computeIfAbsent("snoop", String::isEmpty); This delegation does not need any parameters to be written.

为了更接近你的问题的例子,你可以把它写成 whoLetDogsOut.computeIfAbsent(snoop键 - > tryToLetOut(key)); (无论您是否命名参数 k )。或者写为 whoLetDogsOut.computeIfAbsent(snoop,MyClass :: tryToLetOut); 如果 tryToLetOut static whoLetDogsOut.computeIfAbsent(snoop,this :: tryToLetOut); if tryToLetOut 是一个实例方法。

To be closer to the example in your question, you could write it as whoLetDogsOut.computeIfAbsent("snoop", key -> tryToLetOut(key)); (it doesn’t matter whether you name the parameter k or key). Or write it as whoLetDogsOut.computeIfAbsent("snoop", MyClass::tryToLetOut); if tryToLetOut is static or whoLetDogsOut.computeIfAbsent("snoop", this::tryToLetOut); if tryToLetOut is an instance method.

这篇关于如何使用新的computeIfAbsent函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆