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

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

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

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

  • 如果在 Properties 的 compute() 中传递的重新映射函数返回 null 作为返回值,则映射将从 Properties 中删除(或者如果最初不存在,则仍然不存在)。
  • 如果重新映射函数抛出异常,则重新抛出异常,并且当前映射保持不变。
  • 在计算过程中,不允许使用此方法修改此映射。
  • 如果重映射函数在计算期间修改了此映射,则此方法将抛出ConcurrentModificationException

句法:

public Object compute?(Object key,
                      BiFunction remappingFunction)

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

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

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

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

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

方案一:

// Java program to demonstrate
// compute(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());
  
        // remap the values of Properties
        // using compute method
        properties.compute("Pen", (key, val)
                                      -> 15);
        properties.compute("Clothes", (key, val)
                                          -> 120);
  
        // print new mapping
        System.out.println("New Properties: "
                           + properties.toString());
    }
}
输出:
Current Properties: {Book=500, Mobile=5000, Pen=10, Clothes=400}
New Properties: {Book=500, Mobile=5000, Pen=15, Clothes=120}

方案二:

// Java program to demonstrate
// compute(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());
  
        // remap the values of Properties
        // using compute method
        properties.compute(3, (key, val)
                                  -> "00RS");
        properties.compute(2, (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=$, 1=100RS}

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