Java ConcurrentHashMap compute()方法及示例

Java ConcurrentHashMap compute()方法及示例

ConcurrentHashMap类compute(Key, BiFunction) 方法用于计算指定键和其当前映射值的映射(如果没有找到当前映射,则为空)。

  • 该方法用于原子化地更新ConcurrentHashMap中给定键的值。
  • 如果重映射函数抛出一个异常,该异常将被重新抛出,而当前的映射将保持不变。
  • 在计算过程中,其他线程对该映射的更新过程被阻断,所以计算时不能试图更新该映射的任何其他映射。
  • 例如,这个映射追加了映射的字符串值。
ConcurrentHashMap.compute(key, 
(key, value) -> (value == null) ? msg : value.concat(msg))

语法

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

参数: 该方法接受两个参数。

  • key :将与该值关联的键。
  • remappingFunction :对值进行操作的函数。

返回: 该方法返回 与指定的键相关联的新值,如果没有则返回空值。异常: 该方法抛出以下异常。

  • NullPointerException: 如果指定的键或remappingFunction为空。
  • llegalStateException: 如果计算试图对这个地图进行递归更新,否则将无法完成。
  • RuntimeException: 如果remappingFunction这样做了,在这种情况下,映射就不会改变。

下面的程序说明了 compute(Key, BiFunction) 方法:

程序 1 :

// 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);
    }
}

输出

ConcurrentHashMap: {Book3=400, Book1=10, Book2=500}
New ConcurrentHashMap: {Book3=400, Book1=522, Book2=600}

程序2

// 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);
    }
}

输出

ConcurrentHashMap: {1=Kolkata, 2=Nadia, 3=Howrah}
New ConcurrentHashMap: {1=Kolkata, 2=Nadia (West-Bengal), 3=Howrah (West-Bengal)}

程序3: 显示NullPointerException

// 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);
        }
    }
}

输出

Exception: java.lang.NullPointerException

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程