Java ConcurrentHashMap putIfAbsent() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21621803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:42:09  来源:igfitidea点击:

ConcurrentHashMap putIfAbsent() is returning null

javaconcurrenthashmap

提问by Niranjan

The following program is printing NULL. I am not able to understand why.

以下程序正在打印 NULL。我不明白为什么。

public class ConcurrentHashMapTest {
    public static final ConcurrentMap<String, String> map = new ConcurrentHashMap<>(5, 0.9f, 2);

    public static void main(String[] args) {
        map.putIfAbsent("key 1", "value 1");
        map.putIfAbsent("key 2", "value 2");

        String value = get("key 3");
        System.out.println("value for key 3 --> " + value);
    }

    private static String get(final String key) {
        return map.putIfAbsent(key, "value 3");
    }
}

Could someone help me understand the behavior?

有人可以帮助我理解这种行为吗?

采纳答案by Evgeniy Dorofeev

ConcurrentMap.putIfAbscent returns the previous value associated with the specified key, or null if there was no mapping for the key. You did not have a value associated with "key 3". All correct

ConcurrentMap.putIfAbscent 返回与指定键关联的前一个值,如果键没有映射,则返回 null。您没有与“key 3”相关联的值。全都正确

回答by Narendra Pathai

putIfAbsent()returns the previous value associated with the specified key, or nullif there was no mapping for the key, and because key 3is not present in the map so it returns null.

putIfAbsent()返回与指定键关联的先前值,或者null如果该键没有映射,并且由于映射key 3中不存在,则返回null

You have added key 1and key 2in the map but key 3is not associated with any value. So you get a null. Map key 3with some value and putIfAbsent()will return previous value associated with that key.

您已在地图中添加key 1key 2但未key 3与任何值关联。所以你得到一个null. key 3使用某个值映射putIfAbsent()并将返回与该键关联的先前值。

Like if map already contained key 3associated with value A

就像如果地图已经包含key 3与值相关联A

key 3 ---> A

Then on calling map.putIfAbsent("key 3","B")will return A

然后在调用时map.putIfAbsent("key 3","B")会返回A

回答by Mark Rotteveel

Please read the documentation of ConcurrentHashMap.putIfAbsent:

请阅读以下文档ConcurrentHashMap.putIfAbsent

Returns:
the previous value associated with the specified key, or nullif there was no mapping for the key

返回
与指定键关联的前一个值,或者null如果该键没有映射

As there was no previous value for the key "key 3", it returns null.

由于该键没有以前的值"key 3",所以它返回null

回答by Bozho

It's in the javadoc:

它在 javadoc 中:

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

返回与指定键关联的前一个值,如果没有该键的映射,则返回 null

回答by Ankur Shanbhag

If you look at the documentation, it says

如果您查看文档,它会说

Returns:
the previous value associated with the specified key, or null if there was no mapping for the key

返回:
与指定键关联的前一个值,如果该键没有映射,则返回null

In your case, no value was previously associated with the key, hence NULL

在您的情况下,以前没有与键关联的值,因此 NULL

回答by Jose H. Martinez

This is a frequently asked question, which perhaps suggest this behaviour is unintuitive. Maybe the confusion comes from the way dict.setdefault() works in python and other languages. Returning the same object you just put helps cut a few lines of code.

这是一个常见问题,这可能表明这种行为是不直观的。也许混乱来自 dict.setdefault() 在 python 和其他语言中的工作方式。返回刚刚放置的同一个对象有助于减少几行代码。

Consider:

考虑:

if (map.contains(value)){
    obj = map.get(key);
}
else{
    obj = new Object();
}

versus:

相对:

obj = map.putIfAbsent(key, new Object());

回答by Ond?ej Sta?ek

Problem is that by definition putIfAbsentreturn old value and not new value (old value for absent is always null). Use computeIfAbsent- this will return new value for you.

问题是根据定义putIfAbsent返回旧值而不是新值(不存在的旧值始终为空)。使用computeIfAbsent- 这将为您返回新值。

private static String get(final String key) {
    return map.computeIfAbsent(key, s -> "value 3");
}