A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/concurrenthashmap-compute-method-in-java-with-examples/ below:

ConcurrentHashMap compute() method in Java with Examples

ConcurrentHashMap compute() method in Java with Examples

Last Updated : 29 Jun, 2022

The compute(Key, BiFunction) method of ConcurrentHashMap class is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found).

ConcurrentHashMap.compute(key, 
(key, value) -> (value == null) ? msg : value.concat(msg))

Syntax:

public V 
       compute(K key,
               BiFunction<? super K, ? super V, 
                  ? extends V> remappingFunction)

Parameters: This method accepts two parameters:

Returns: This method returns new value associated with the specified key, or null if none. Exception: This method throws following exceptions:

Below programs illustrate the compute(Key, BiFunction) method: Program 1: 

Java
// Java program to demonstrate
// compute(Key, BiFunction) method.

import java.util.concurrent.*;

public class GFG {

    // Main method
    public static void main(String[] args)
    {

        // create a ConcurrentHashMap and add some values
        ConcurrentHashMap<String, Integer>
            map = new ConcurrentHashMap<>();
        map.put("Book1", 10);
        map.put("Book2", 500);
        map.put("Book3", 400);

        // print map details
        System.out.println("ConcurrentHashMap: "
                           + map.toString());

        // remap the values of ConcurrentHashMap
        // using compute method
        map.compute("Book2", (key, val)
                                 -> val + 100);
        map.compute("Book1", (key, val)
                                 -> val + 512);

        // print new mapping
        System.out.println("New ConcurrentHashMap: "
                           + map);
    }
}
Output:
ConcurrentHashMap: {Book3=400, Book1=10, Book2=500}
New ConcurrentHashMap: {Book3=400, Book1=522, Book2=600}

Program 2: 

Java
// Java program to demonstrate
// compute(Key, BiFunction) method.

import java.util.concurrent.*;

public class GFG {

    // Main method
    public static void main(String[] args)
    {

        // create a ConcurrentHashMap and add some values
        ConcurrentHashMap<Integer, String>
            map = new ConcurrentHashMap<>();
        map.put(1, "Kolkata");
        map.put(2, "Nadia");
        map.put(3, "Howrah");

        // print map details
        System.out.println("ConcurrentHashMap: "
                           + map.toString());

        // remap the values of ConcurrentHashMap
        // using compute method
        map.compute(2, (key, val)
                           -> val.concat(" (West-Bengal)"));
        map.compute(3, (key, val)
                           -> val.concat(" (West-Bengal)"));

        // print new mapping
        System.out.println("New ConcurrentHashMap: "
                           + map);
    }
}
Output:
ConcurrentHashMap: {1=Kolkata, 2=Nadia, 3=Howrah}
New ConcurrentHashMap: {1=Kolkata, 2=Nadia (West-Bengal), 3=Howrah (West-Bengal)}

Program 3:To show NullPointerException 

Java
// Java program to demonstrate NullPointerException
// for compute(Key, BiFunction) method.

import java.util.concurrent.*;

public class GFG {

    // Main method
    public static void main(String[] args)
    {

        // create a ConcurrentHashMap and add some values
        ConcurrentHashMap<Integer, String>
            map = new ConcurrentHashMap<>();
        map.put(1, "Kolkata");
        map.put(2, "Nadia");
        map.put(3, "Howrah");

        try {

            // remap the values of ConcurrentHashMap
            // using compute method
            map.compute(null, (key, val)
                                  -> val.concat(" (West-Bengal)"));
        }
        catch (NullPointerException e) {

            // print Exception
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception: java.lang.NullPointerException

References: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#compute-K-java.util.function.BiFunction-



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4