Java Map compute()方法示例

x33g5p2x  于2022-09-24 转载在 Java  
字(2.9k)|赞(0)|评价(0)|浏览(220)

computejava.util.Map 的默认方法,已在 Java 8 中引入。compute 方法尝试Compute指定键及其当前Map值的Map。从 Java 文档中找到方法声明。

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

key 是要关联指定值的键。
remappingFunction 是Compute值的重Map函数。
compute 方法返回与指定键关联的新值,如果没有则返回 null。

compute 方法的工作原理如下。
1. 如果指定键的旧值和重新Map函数Compute的新值都不为空,在这种情况下,旧值将被新值替换。
2. 如果指定键的旧值不为空,但重新Map函数Compute的新值为空,在这种情况下,指定键的条目将被删除。
3. 如果指定键的旧值为空,但重新Map函数Compute的新值不为空,在这种情况下,旧值将被新值替换。
4. 如果指定键的旧值为空,并且通过重Map函数Compute的新值也为空,在这种情况下,指定键的条目将被删除。

示例-1

旧值不为空。
新值也不为空。
在这种情况下,旧值将被新值替换。
Compute1.java

import java.util.HashMap;
import java.util.Map;

public class Compute1 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     
     String newValue = studentMap.compute(101, (k, v) -> v + "-" + k);
     
     System.out.println(newValue);
     System.out.println(studentMap);
  }
}

输出

Mahesh-101
{101=Mahesh-101, 102=Suresh}

不使用以下 compute 方法也可以实现相同的目的。
Compute11.java

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class Compute11 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     
     BiFunction<Integer, String, String> remappingFunction = (k, v) -> v + "-" + k;
     int key = 101;
     String oldValue = studentMap.get(key);
     String newValue = remappingFunction.apply(key, oldValue);
     
     if (oldValue != null) {
        if (newValue != null)
          studentMap.put(key, newValue);
        else
          studentMap.remove(key);
     } else {
        if (newValue != null)
          studentMap.put(key, newValue);
        else
          studentMap.remove(key);
     }
     
     System.out.println(newValue);
     System.out.println(studentMap);
  }
}

输出

Mahesh-101
{101=Mahesh-101, 102=Suresh}

示例-2

旧值不为空。
新值为空。
在这种情况下,指定键的条目将被删除。
Compute2.java

import java.util.HashMap;
import java.util.Map;

public class Compute2 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
    
     studentMap.compute(102, (k, v) -> null);
     
     System.out.println(studentMap);
  }
}

输出

{101=Mahesh}

示例-3

旧值为空。
新值不为空。
在这种情况下,旧值将被新值替换。
Compute3.java

import java.util.HashMap;
import java.util.Map;

public class Compute3 {
  public static void main(String[] args) {
     Map<String, String> map = new HashMap<>();
     map.put("Bharat", "Modi");
     map.put("Russia", null);
  
     map.compute("Bharat", (k, v) -> "Mr. ".concat(v));
     map.compute("Russia", (k, v) -> v == null ? "Mr. Putin" : "Mr. ".concat(v));
     
     System.out.println(map);
  }
}

输出

{Bharat=Mr. Modi, Russia=Mr. Putin}

示例-4

旧值为空。
新值也为空。
在这种情况下,指定键的条目将被删除。
Compute4.java

import java.util.HashMap;
import java.util.Map;

public class Compute4 {
  public static void main(String[] args) {
     Map<String, String> map = new HashMap<>();
     map.put("Bharat", "Modi");
     map.put("Russia", null);
  
     map.compute("Russia", (k, v) -> null);
     
     System.out.println(map);
  }
}

输出

{Bharat=Modi}

相关文章

微信公众号

最新文章

更多