• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

ComputeIfPresent() method

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading the nice  book of Jeanne Boyarsky and Scott Selikoff (The study Guide for OCP).
But I have an issue with this:

Let suppoze that this is the contain of a map m: [jenny=null]
If we apply m.computeIfPresent("jenny", (k,v) -> null), from what I understand the pair key/value is going to be deleted.

I am right? because the recap in page 159 is the contrary.

I would like to be fixed please.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hervé Lionel Ondoua wrote:Let suppoze that this is the contain of a map m: [jenny=null]
If we apply m.computeIfPresent("jenny", (k,v) -> null), from what I understand the pair key/value is going to be deleted.


That is incorrect. Nothing will happen.

The API documentation of Map.computeIfPresent() explains this:


If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
If the function returns null, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.


In your example, the value for the key "jenny" is null, so the mapping function is not called. Since the function is not called at all, the second sentence ("if the function returns null") is not applicable for this case - so the mapping is not removed.

You can try this out for yourself by writing a small program:

Output:

{jenny=null}
{jenny=null}


Note that on the second line, you see that the entry is still there; it has not been deleted.
 
Hervé Lionel Ondoua
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper de Jong.

Thus no matter if the mapping Bifunction returns null or not, since the value is null the computeIfpresent() does not change anything on the map, is that right?
 
Saloon Keeper
Posts: 12006
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Try it out with the compute() method instead of the computeIfPresent() method.
 
I am Arthur, King of the Britons. And this is a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic