You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/07/23 16:25:00 UTC
[jira] [Updated] (GROOVY-10508) DefaultGroovyMethods.get() unexpectedly puts the default value into the origin map
[ https://issues.apache.org/jira/browse/GROOVY-10508?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles updated GROOVY-10508:
---------------------------------
Fix Version/s: (was: 5.0.0-alpha-1)
> DefaultGroovyMethods.get() unexpectedly puts the default value into the origin map
> ----------------------------------------------------------------------------------
>
> Key: GROOVY-10508
> URL: https://issues.apache.org/jira/browse/GROOVY-10508
> Project: Groovy
> Issue Type: Documentation
> Reporter: Shi Tailong
> Assignee: Paul King
> Priority: Minor
> Fix For: 4.0.1
>
>
>
> {code:java}
> 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);
> } {code}
> Above there's the get() method for ALL maps if there's more than one arguments for this. You can find that it will put the defaultValue into the map if the key doesn't hit.
>
> It seems not to be an obvious choice for programmers, as the origin map would be CHANGED when the programmer may just want a default value.
> Meanwhile, some of the implements of Map have some restrictions for the values. For example, Redisson doesn't allow null values for its RMap class. So when you're using Redisson in Groovy like this,
>
> {code:java}
> RMapCache<String, String> map = redissonClient.getMapCache(SOME_KEY)
> String cachedValue = map.getOrDefault(key, null) {code}
> it will return a NullPointerError like "map value can't be null".
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)