📌  相关文章
📜  Java中的属性 computeIfPresent(Key, BiFunction) 方法及示例

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

Java中的属性 computeIfPresent(Key, BiFunction) 方法及示例

Properties 类computeIfPresent(Key, BiFunction)方法允许您计算指定键的映射值,如果键已经与一个值相关联(或映射到空值)。

  • 如果此方法的映射函数返回 null,则删除映射。
  • 如果重新映射函数抛出异常,则重新抛出异常,并且映射保持不变。
  • 在计算过程中,不允许使用此方法修改此映射。
  • 如果重映射函数在计算期间修改了此映射,则此方法将抛出 ConcurrentModificationException。

句法:

public Object computeIfPresent?(Object key,
                              BiFunction remappingFunction)

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

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

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

异常:如果检测到重映射函数修改了此映射,则此方法抛出ConcurrentModificationException

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

方案一:

// Java program to demonstrate
// computeIfPresent(Key, BiFunction) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Create a properties and add some values
        Properties properties = new Properties();
        properties.put("Pen", 10);
        properties.put("Book", 500);
        properties.put("Clothes", 400);
        properties.put("Mobile", 5000);
  
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
  
        // provide new value for keys which is present
        // using computeIfPresent method
        properties.computeIfPresent("Pen", (key, val)
                                               -> 600);
        properties.computeIfPresent("Book", (key, val)
                                                -> 800);
  
        // print new mapping
        System.out.println("New Properties: "
                           + properties.toString());
    }
}
输出:
Current Properties: {Book=500, Mobile=5000, Pen=10, Clothes=400}
New Properties: {Book=800, Mobile=5000, Pen=600, Clothes=400}

方案二:

// Java program to demonstrate
// computeIfPresent(Key, BiFunction) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Create a properties and add some values
        Properties properties = new Properties();
  
        properties.put(1, "100RS");
        properties.put(2, "500RS");
        properties.put(3, "1000RS");
  
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
  
        // provide new value for key which is present
        // using computeIfPresent method
        properties.computeIfPresent(3, (key, val)
                                           -> "00RS");
  
        // this will not effect anything
        // because key 4 is absent
        properties.computeIfPresent(4, (key, val)
                                           -> "$");
  
        // print new mapping
        System.out.println("New Properties: "
                           + properties.toString());
    }
}
输出:
Current Properties: {3=1000RS, 2=500RS, 1=100RS}
New Properties: {3=00RS, 2=500RS, 1=100RS}

参考资料:https://docs.oracle.com/javase/9/docs/api/ Java/util/Properties.html#computeIfPresent-java.lang.Object-java.util。函数.BiFunction-