Java Programming Tutorials

Java programming tutorials with many code examples!

Java Util Map computeIfAbsent(key,Function)

java.util.Map.computeIfAbsent(key, Function)

The method Map.computeIfAbsent(key, Function) computes the new value and associates it with the key if there is no mapping for the given key or the mapping is null. Returns the new value associated with the key.

If the mapping function will return null the value will not be put into the map.

Method signature

The signature of the java.util.Map.computeIfAbsent(key, Function) method is as follows:

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

The method is available since Java 8.

Parameters

  • K key: key with which to associate the value,
  • Function mappingFunction: function to compute a value.

Return value

The new value associated with the specified key, or null if none.

Exceptions

  • NullPointerException: when mappingFunction is null or if the specified key is null and this map does not support null keys,
  • UnsupportedOperationException: if the put operation is not supported by this map (optional)
  • ClassCastException: if the class of the specified key or value prevents it from being stored in this map (optional)

Example usage

In the following code we use java.util.Map.computeIfAbsent(key, Function):

package com.farenda.java.util;

import java.util.HashMap;
import java.util.Map;

public class MapExamples {

    public static void main(String[] args) {
        // updates:
        Map<Integer,String> nameForId = new HashMap<>();
        nameForId.put(1, "Java");
        nameForId.put(2, null);
        System.out.println("Original map: " + nameForId);

        nameForId.computeIfAbsent(1, key -> "Java at " + key);
        System.out.println("No changes: " + nameForId);

        nameForId.computeIfAbsent(2, key -> "Esperanto at " + key);
        System.out.println("Null updated: " + nameForId);

        nameForId.computeIfAbsent(3, key -> "Clojure at "+ key);
        System.out.println("New key: " + nameForId);
    }
}

The above code produces the following output:

Original map: {1=Java, 2=null}
No changes: {1=Java, 2=null}
Null updated: {1=Java, 2=Esperanto at 2}
New key: {1=Java, 2=Esperanto at 2, 3=Clojure at 3}

In the following code we use computeIfAbsent to implement multimap:

package com.farenda.java.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MapExamples {

    public static void main(String[] args) {
        Map<String,Collection<String>> multimap = new HashMap<>();

        multimap.computeIfAbsent("names", key -> new ArrayList<>())
                .add("Kajko");
        System.out.println("multimap: " + multimap);

        multimap.computeIfAbsent("names", key -> new ArrayList<>())
                .add("Kokosz");
        System.out.println("multimap: " + multimap);
    }
}

The above code produces the following result:

multimap: {names=[Kajko]}
multimap: {names=[Kajko, Kokosz]}
Share with the World!