Java 8 Map中的putIfAbsent和computeIfAbsent有什么区别? [英] What is the difference between putIfAbsent and computeIfAbsent in Java 8 Map ?

查看:541
本文介绍了Java 8 Map中的putIfAbsent和computeIfAbsent有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读有趣的文章的人声称这两个功能之间的区别是:

Reading an interesting articles the guys claims that the difference between the two function are:

两个函数都希望在未指定Key的情况下添加元素 已经存在于地图中.

Both functions aspire to add an element if the specified Key is not already present in Map.

putIfAbsent添加具有指定值的元素,而 computeIfAbsent添加一个元素,该元素具有使用键计算的值. http://www.buggybread.com/2014/10/java-8-difference-between-map.html

putIfAbsent adds an element with the specified Value whereas computeIfAbsent adds an element with the value computed using the Key. http://www.buggybread.com/2014/10/java-8-difference-between-map.html

还有

我们已经看到,putIfAbsent消除了必须 定义if语句,但是如果获取Java文章该怎么办呢? 真的伤害了我们的表现吗?

We’ve seen that putIfAbsent removes the imperative way of having to define the if-statement, but what if fetching the Java articles is really hurting our performance?

要对此进行优化,我们不希望在获取以下内容之前先获取文章 真的确定我们需要它们-这意味着我们需要知道关键是 在获取文章之前缺席. http://www.deadcoderising.com/2017-02-14-java-8-declarative-ways-of-modifying-a-map-using-compute-merge-and-replace/

To optimise this, we don’t want to fetch the articles until we’re really sure we need them — meaning we need to know if the key is absent before fetching the articles. http://www.deadcoderising.com/2017-02-14-java-8-declarative-ways-of-modifying-a-map-using-compute-merge-and-replace/

我还没准备好理解有什么区别,请您详细说明这两个功能吗?

I didn't ready understand what are the differences can you please elaborate more on these two functions ?

推荐答案

差异#1

computeIfAbsent takes a mapping function, that is called to obtain the value if the key is missing.

putIfAbsent 直接获取值.

putIfAbsent takes the value directly.

如果获取该值昂贵,那么putIfAbsent会浪费该密钥(如果该密钥已经存在).

If the value is expensive to obtain, then putIfAbsent wastes that if the key already exists.

常见的昂贵"值为例如new ArrayList<>()用于创建Map<K, List<V>>时,当键已经存在时创建新列表(然后丢弃新列表)会产生不必要的垃圾.

A common "expensive" value is e.g. new ArrayList<>() for when you're creating a Map<K, List<V>>, where creating a new list when the key already exists (which then discards the new list) generates unnecessary garbage.

差异#2

computeIfAbsent returns "the current (existing or computed) value associated with the specified key, or null if the computed value is null".

putIfAbsent 返回与指定键关联的先前值;如果键没有映射,则返回null".

putIfAbsent returns "the previous value associated with the specified key, or null if there was no mapping for the key".

因此,如果键已经存在,则它们返回相同的内容,但是如果键丢失,则computeIfAbsent返回计算值,而putIfAbsent返回null.

So, if the key already exists, they return the same thing, but if the key is missing, computeIfAbsent returns the computed value, while putIfAbsent return null.

差异#3

这两种方法都将缺少"定义为缺少键或现有值为null,但是:

Both method define "absent" as key missing or existing value is null, but:

computeIfAbsent 不会放置空值.

computeIfAbsent will not put a null value if the key is absent.

putIfAbsent 也会保留键的值.

putIfAbsent will put the value if the key is absent, even if the value is null.

对于将来的

It makes no difference for future calls to computeIfAbsent, putIfAbsent, and get calls, but it does make a difference to calls like getOrDefault and containsKey.

这篇关于Java 8 Map中的putIfAbsent和computeIfAbsent有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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