Testlib: Add test that putIfAbsent replaces a null value
See original GitHub issueThis was shown in a recent Java Collections Puzzlers. The JavaDoc for Map.putIfAbsent
states the following.
If the specified key is not already associated with a value (or is mapped to {@code null}) associates it with the given value and returns {@code null}, else returns the current value.
@Test
public void putIfAbsent_nullExistingValue() {
var map = new HashMap<Object, Object>();
map.put("a", null);
map.putIfAbsent("a", "b");
assertThat(map).containsEntry("a", "b");
}
I found that multiple custom maps which permit null values do not honor this peculiarity (and similar for computeIfAbsent
). For example fastutils’ adopted the testlib and Object2ObjectOpenHashMap
passes its suite, but it mistakenly does not replace the value as shown above.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:19 (12 by maintainers)
Top Results From Across the Web
Why Map.putIfAbsent() is returning null? - Stack Overflow
Problem is that by definition putIfAbsent return old value and not new value (old value for absent is always null). Use computeIfAbsent -...
Read more >HashMap putIfAbsent(key, value) method in Java with Examples
This method returns null (if there was no mapping with the provided key before or it was mapped to a null value) or...
Read more >[JavaSpecialists 303] - Null Keys and Values in Maps
Some Map implementations allow null keys and values. This leads to funky behaviour when calling putIfAbsent() and the compute functions.
Read more >Solved: Replace null with a value - Power Platform Community
Solved: Using a Compose action, how can I when a null value is ... if(equals(<VALUE TO TEST>,null),'Value if null','value if not null').
Read more >Replace put(..) with putIfAbsent(..) - jSparrow Documentation
Including Null-Checks. Pre. V v = map.get(key); if (v == null) { v = map.put(key, value); } return v;. Post. map.putIfAbsent(key, value); return...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The segment style in Guava’s is forked from Java 5’s ConcurrentHashMap. There are definite gotchas with atomicity and null values, and I think allowing null values at all was a mistake for Map. It is best replaced by a sentinel value, e.g. Optional, which has the same cpu and memory footprint (as no one actually makes a valueless entry object for this case in their maps). My general rule is to follow Doug Lea’s decisions, as if he found a feature too hard and confusing to support then everyone else will get it wrong (either the implementor or user, as too much of a footgun for a consistent understanding).
I think you can use Maven toolchains for handling this, but I am rusty with that build tool.