Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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: 10750
229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Try it out with the compute() method instead of the computeIfPresent() method.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!