What is the difference between putIfAbsent and computeIfAbsent in Java 8 Map ?

We Are Going To Discuss About What is the difference between putIfAbsent and computeIfAbsent in Java 8 Map ?. So lets Start this Java Article.

What is the difference between putIfAbsent and computeIfAbsent in Java 8 Map ?

Advertisements
  1. What is the difference between putIfAbsent and computeIfAbsent in Java 8 Map ?

    computeIfAbsent takes a mapping function, that is called to obtain the value if the key is missing.
    putIfAbsent takes the value directly.
    If the value is expensive to obtain, then putIfAbsent wastes that if the key already exists.

  2. What is the difference between putIfAbsent and computeIfAbsent in Java 8 Map ?

    computeIfAbsent takes a mapping function, that is called to obtain the value if the key is missing.
    putIfAbsent takes the value directly.
    If the value is expensive to obtain, then putIfAbsent wastes that if the key already exists.

Solution 1

Advertisements

Difference #1

computeIfAbsent takes a mapping function, that is called to obtain the value if the key is missing.

putIfAbsent takes the value directly.

If the value is expensive to obtain, then putIfAbsent wastes that if the key already exists.

A common “expensive” value is e.g. new ArrayList<>() for when you’re creating a Map<K, List<V>>, where creating a new list when the key already exists (which then discards the new list) generates unnecessary garbage.


Difference #2

computeIfAbsent returns “the current (existing or computed) value associated with the specified key, or null if the computed value is null”.

putIfAbsent returns “the previous value associated with the specified key, or null if there was no mapping for the key”.

So, if the key already exists, they return the same thing, but if the key is missing, computeIfAbsent returns the computed value, while putIfAbsent return null.


Difference #3

Both method define “absent” as key missing or existing value is null, but:

computeIfAbsent will not put a null value if the key is absent.

putIfAbsent will put the value if the key is absent, even if the value is null.

It makes no difference for future calls to computeIfAbsent, putIfAbsent, and get calls, but it does make a difference to calls like getOrDefault and containsKey.

Original Author Andreas Of This Content

Solution 2

Advertisements

Suppose you have a Map<String,ValueClass>.

map.putIfAbsent("key", new ValueClass());

will create a ValueClass instance anyway, even if the “key” key is already in the Map. This would just create an unnecessary instance.

On the other hand

map.computeIfAbsent("key", k -> new ValueClass());

will only create a ValueClass instance if the “key” key is not already in the Map (or is mapped to a null value).

Therefore computeIfAbsent is more efficient.

putIfAbsent is equivalent to:

ValueClass value = new ValueClass();
if (map.get("key") == null) {
    map.put("key",value);
}

while computeIfAbsent is equivalent to:

if (map.get("key") == null) {
    map.put("key",new ValueClass());
}

Another small difference between the two methods is that computeIfAbsent will not put a null value for an absent key. putIfAbsent will.

Original Author Eran Of This Content

Solution 3

Advertisements

You can understand the difference by carefully looking at the method signatures:

  • putIfAbsent takes a key and value, and puts the value in the map if there is no value for that key in the map.
  • computeIfAbsent takes a key and a Function. If there is no value for that key in the map, the function is called to create the value, which is then put in the map.

If you already have the value, use putIfAbsent.

If you don’t have the value yet and creating the value is an expensive operation (for example, the value has to be looked up in a database), then use computeIfAbsent, so that the expensive operation doesn’t need to be performed in case the map already contains a value for the specified key.

Original Author Jesper Of This Content

Solution 4

Advertisements

Maybe the default implementations can clarify a little bit more….

default V putIfAbsent​(K key, V value)
The default implementation is equivalent to, for this map:

 V v = map.get(key);
  if (v == null)
      v = map.put(key, value);
  return v;

On the other hand:

default V computeIfAbsent​(K key,
                          Function<? super K,? extends V> mappingFunction)

is equivalent to:

if (map.get(key) == null) {
     V newValue = mappingFunction.apply(key);
     if (newValue != null)
         map.put(key, newValue);
}

Original Author zlakad Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment