java Java中HashMap的getOrDefault()和putIfAbsent()哪个更好

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29539336/
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-11-02 15:27:55  来源:igfitidea点击:

Which one is better getOrDefault() or putIfAbsent() of HashMap in Java

javadictionary

提问by shyam karwa

If I have to set values for a key (for many keys) in HashMap if not present then which one is better to use. getOrDefault() or putIfAbsent()As both the method will return the value associated with the key if it is already set. And both will take key,value pair as parameter.

如果我必须为 HashMap 中的一个键(许多键)设置值,如果不存在,那么使用哪个更好。 getOrDefault() 或 putIfAbsent()由于这两种方法都将返回与键关联的值(如果它已经设置)。并且两者都将键值对作为参数。

回答by Jean-Fran?ois Savard

Yes, they will both return the value associated with the key if it is already set, but one is only a getter while the other one is a setter.

是的,如果键已经设置,它们都将返回与键关联的值,但一个只是一个 getter 而另一个是 setter。

putIfAbsent

放置如果不存在

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

如果指定的键尚未与值关联(或映射为 null),则将其与给定值关联并返回 null,否则返回当前值。



getOrDefault

获取或默认值

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

返回指定键映射到的值,如果此映射不包含键的映射,则返回 defaultValue。



If your goal is only to retrieve the value, then use getOrDefault. Else, if you want to set the value when it does not exist, use putIfAbsent.

如果您的目标只是检索值,则使用getOrDefault. 否则,如果要在值不存在时设置该值,请使用putIfAbsent.

According to your first sentence,

根据你的第一句话,

If I have to set values for a key (for many keys) in HashMap if not present then which one is better

如果我必须在 HashMap 中为一个键(许多键)设置值,如果不存在,那么哪个更好

you should use putIfAbsent.

你应该使用putIfAbsent.

回答by Mark

In Java8 There is also computeIfAbsentwhich returns the value if present or if it is absent it creates it through a lambda function, adds it to the Mapand returns its value.

在 Java8 中computeIfAbsent,如果存在或不存在则返回值,它通过 lambda 函数创建它,将其添加到Map并返回其值。

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

回答by Andy Turner

getOrDefault()doesn't mutate the map, so you won't find the values in the map if you subsequently inspected its contents, e.g.

getOrDefault()不会改变地图,因此如果您随后检查其内容,则不会在地图中找到值,例如

HashMap<String, String> map = new HashMap<>();
map.getOrDefault("something", "default");  // returns "default"
assertTrue(map.isEmpty());

putIfAbsent()does mutate the map, so you would find the values in the map if you subsequently inspected its contents, e.g.

putIfAbsent()确实改变了地图,因此如果您随后检查其内容,您将在地图中找到值,例如

HashMap<String, String> map = new HashMap<>();
map.putIfAbsent("something", "default");
assertFalse(map.isEmpty());

You should pick the one appropriate to your needs.

你应该选择一个适合你的需要。