Java ConcurrentHashMap computeIfAbsent()方法及示例

Java ConcurrentHashMap computeIfAbsent()方法及示例

ConcurrentHashMap类computeIfAbsent(Key, Function) 方法,如果key 尚未与一个值相关联 (或被映射为null),则尝试使用给定的映射函数对指定的key计算其值,并在map中输入该计算值,否则为空。

  • 如果这个方法的映射函数返回null,那么在map中就没有记录新键的值。
  • 该方法用于原子化地更新ConcurrentHashMap中给定键的值。
  • 如果重映射函数抛出一个异常,该异常将被重新抛出,并且不记录任何映射。
  • 在计算过程中,不允许使用该方法修改该映射,因为其他线程也可以使用该映射。

语法:

public V 
       computeIfAbsent(K key,
             Function<? super K, ? extends V> remappingFunction)

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

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

返回: 该方法返回 与指定键相关的当前(现有的或计算的)值,如果映射返回空值,则返回空值。
异常: 该方法抛出。

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

下面的程序说明了computeIfAbsent(Key, Function)方法:

程序1:

// Java program to demonstrate
// computeIfAbsent(Key, Function) 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("Pencil", 1000);
        map.put("Laptop", 55000);
        map.put("Clothes", 4400);
        map.put("Mobile", 5300);
 
        // print map details
        System.out.println("ConcurrentHashMap :\n "
                           + map.toString());
 
        // provide value for new key which is absent
        // using computeIfAbsent method
        map.computeIfAbsent("PC", k -> 60000);
        map.computeIfAbsent("Charger", k -> 800);
 
        // print new mapping
        System.out.println("new ConcurrentHashMap :\n "
                           + map);
    }
}

输出

ConcurrentHashMap :
 {Laptop=55000, Clothes=4400, Pencil=1000, Mobile=5300}
new ConcurrentHashMap :
 {Laptop=55000, PC=60000, Clothes=4400, Pencil=1000, Charger=800, Mobile=5300}

程序2:

// Java program to demonstrate
// computeIfAbsent(Key, Function) 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, "1000RS");
        map.put(2, "5009RS");
        map.put(3, "1300RS");
 
        // print map details
        System.out.println("ConcurrentHashMap : \n"
                           + map.toString());
 
        // provide value for new key which is absent
        // using computeIfAbsent method
        map.computeIfAbsent(4, k -> "6000RS");
 
        // this will not effect anything
        // because key 1 is present
        map.computeIfAbsent(1, k -> "8000RS");
 
        // print new mapping
        System.out.println("new ConcurrentHashMap :\n"
                           + map);
    }
}

输出

ConcurrentHashMap : 
{1=1000RS, 2=5009RS, 3=1300RS}
new ConcurrentHashMap :
{1=1000RS, 2=5009RS, 3=1300RS, 4=6000RS}

程序3: 显示NullPointerException

// Java program to demonstrate NullPointerException of
// computeIfAbsent(Key, Function) 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("Pencil", 1000);
        map.put("Laptop", 55000);
        map.put("Clothes", 4400);
        map.put("Mobile", 5300);
 
        try {
 
            // provide value for new key which is absent
            // using computeIfAbsent method
            map.computeIfAbsent(null, k -> 60000);
        }
        catch (Exception e) {
 
            // print new mapping
            System.out.println("Exception: "
                               + e);
        }
    }
}

输出

Exception: java.lang.NullPointerException

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程