question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testlib: Add test that putIfAbsent replaces a null value

See original GitHub issue

This 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:open
  • Created a year ago
  • Reactions:1
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
ben-manescommented, Nov 13, 2022

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).

1reaction
ben-manescommented, Oct 27, 2022

I think you can use Maven toolchains for handling this, but I am rusty with that build tool.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found