📜  Java中的 ConcurrentHashMap compute() 方法及示例

📅  最后修改于: 2022-05-13 01:55:13.257000             🧑  作者: Mango

Java中的 ConcurrentHashMap compute() 方法及示例

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

  • 此方法用于自动更新 ConcurrentHashMap 中给定键的值。
  • 如果重新映射函数抛出异常,则重新抛出异常,并且当前映射保持不变。
  • 在计算期间,来自其他线程的此映射的更新过程已被阻止,因此计算不得尝试更新此映射的任何其他映射
  • 例如,此映射附加映射的字符串值:
    ConcurrentHashMap.compute(key, 
    (key, value) -> (value == null) ? msg : value.concat(msg))
    

句法:

public V 
       compute(K key,
               BiFunction remappingFunction)

参数:此方法接受两个参数:

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

返回:此方法返回与指定键关联的新值,如果没有则返回 null

异常:此方法引发以下异常:

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

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

方案一:

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

方案二:

// 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
            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
            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: Java/util/concurrent/ConcurrentHashMap.html#compute-K-java.util。函数.BiFunction-