【问题标题】:How to pass Function as parameter to computeIfAbsent method?如何将函数作为参数传递给 computeIfAbsent 方法?
【发布时间】:2019-07-11 15:09:33
【问题描述】:

我是 Java 新手,有点从 C# 过渡到 Java。 java.util.function 有一个定义为Function 的接口,该接口是MapcomputeIfAbsent 方法的输入。

我想定义该函数并将其委托给computeIfAbsent 方法。

map.computeIfAbsent(key, k => new SomeObject())

工作,但 我希望它与回调 where func. 但问题是Function 需要定义输入参数。如何将其设置为 void 或不带参数。

map.computeIfAbsent(key, func);

【问题讨论】:

  • 为什么“函数需要定义输入参数”是个问题?你能描述一下你真正想做的事情吗?
  • 我的 computeIfAbsent 在循环中。说(25000)次循环执行。如果我每次都按照传统方法创建 lambda 函数,然后将其传递给 computeIfAbsent 函数。为什么应该始终创建它。我们不能把它存储在函数中并传递它。
  • 是的,你可以。如果这是你的问题,你为什么不直接问这个问题,而不是直接问“我如何将它设置为 void 或没有参数”?

标签: java lambda java-8 functional-programming hashmap


【解决方案1】:

如果func 的计算成本不高且没有副作用,那么您可以直接使用putIfAbsent(注意它是'put',而不是'compute')并直接调用该方法。它在语义上是等价的。

map.putIfAbsent(key, func());

func 每次都会被评估,不管它是否要被插入,但只要它很快,那就不是问题。

【讨论】:

  • 只有在func()返回Function<K, V>时才会编译。
  • 抱歉,卡在compute,错过了put
【解决方案2】:

computeIfAbsent 将始终为传递的Function 提供一个输入参数 - 这将是关键。

因此,正如你可以写的那样:

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

你也可以写(假设你的Map的键是String):

Function<String,SomeObject> func = k -> new SomeObject();
map.computeIfAbsent(key, func);

【讨论】:

  • 我正在使用地图收藏。我用 Function func = k -> new SomeObject(); 修改了你的答案因为我的键类型是字符串
  • 您之前的回答很笼统。请只保留它。请使其通用并通过示例添加说明。这将解决目的,也将对其他人有用。函数
【解决方案3】:

computeIfAbsent 有两个参数:

  • key - 与指定值关联的键
  • mappingFunction - 计算值的函数(此类型 Function,一个函数式接口,它接受一个参数和 返回一个结果)。

没有其他方法可以为方法computeIfAbsent 指定参数。下面是一个使用Map 方法的示例:

输入:Map&lt;String, Integer&gt; map: {four=4, one=1, two=2, three=3, five=5}

map.computeIfAbsent("ten", k -> new Integer(10));   // adds the new record to the map
map.computeIfAbsent("twelve", k -> null);           // record is not added, the function returns a null
map.computeIfAbsent("one", k -> new Integer(999999));   // record is not updated, the key "one" already exists -
                                                        // and has a non-null value
map.put("eleven", null);                                // new record with null value
map.computeIfAbsent("eleven", k -> new Integer(11));    // updates the record with new value

输出:{four=4, one=1, eleven=11, ten=10, two=2, three=3, five=5}


考虑代码map.computeIfAbsent("ten", k -&gt; new Integer(10));

代码说我想插入一个新的映射(键值对)到map。方法computeIfAbsent需要指定key,即“十”。 value 派生自Function,它将键作为输入参数并产生结果;结果被设置为映射的值。

在示例中,将键值对添加到映射中:“十”,10。

lambda 表达式 k -&gt; new Integer(10)功能接口 Function&lt;String, Integer&gt; 的一个实例。代码也可以表示为:

Function<String, Integer> fn = k -> new Integer(10);
map.computeIfAbsent("ten", fn);

【讨论】:

    【解决方案4】:

    computeIfAbsent(Key, Function) 用于使用给定的映射函数计算给定键的值,如果键尚未与值关联(或映射为 null)并将计算的值输入到 Hashmap 中,否则为 null。根据语法

    public V computeIfAbsent(K key, Function<? super K, ? extends V> remappingFunction)
    

    你可以写 -

    map.computeIfAbsent(key, k -&gt; Object());

    map.computeIfAbsent(key, k -> fun());
    

    【讨论】:

      【解决方案5】:

      您可以只创建一个接受参数并调用您的函数的 lambda,而忽略参数。

      map.computeIfAbsent(key, k -> func());
      

      【讨论】:

        猜你喜欢
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多