改用 Java 的 Map.getOrDefault(key, value):
mymap = ['key': 'value']
println mymap
v = mymap.getOrDefault('notexistingkey', 'default')
println v
println mymap
输出:
[key:value]
default
[key:value]
Groovy SDK 通过DefaultGroovyMethods.get(map, key, default) 添加Map.get(key, default),如果您看一下Javadoc 所说的内容,您就会明白这种行为是意料之中的:
在给定键的映射中查找项目并返回值 - 除非给定键没有条目,在这种情况下,将默认值添加到映射并返回。
这就是这个方法的实现的样子:
/**
* Looks up an item in a Map for the given key and returns the value - unless
* there is no entry for the given key in which case add the default value
* to the map and return that.
* <pre class="groovyTestCase">def map=[:]
* map.get("a", []) << 5
* assert map == [a:[5]]</pre>
*
* @param map a Map
* @param key the key to lookup the value of
* @param defaultValue the value to return and add to the map for this key if
* there is no entry for the given key
* @return the value of the given key or the default value, added to the map if the
* key did not exist
* @since 1.0
*/
public static <K, V> V get(Map<K, V> map, K key, V defaultValue) {
if (!map.containsKey(key)) {
map.put(key, defaultValue);
}
return map.get(key);
}
这是一个相当古老的概念(从 Groovy 1.0 开始)。但是我建议不要使用它——这个.get(key, default) 操作既不是原子的,也不是同步的。当您在为并发访问而设计的ConcurrentMap 上使用它时,问题就开始了——这种方法违反了它的约定,因为containsKey、put 和最终的get 调用之间没有同步。