当前位置:   article > 正文

Map的compute方法详解(Java)_map compute

map compute

1.compute方法

compute 方法是 Map 接口中的一个方法,用于根据指定的键和计算函数对指定键的值进行修改
方法的签名如下:

default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
  • 1
  • key:要操作的键。
  • remappingFunction:一个接收键和当前键对应的值作为输入,并返回一个新值的函数。

compute 方法的行为取决于指定的键当前是否存在于 Map

  1. 如果指定的键存在且对应的值不为 null,则将键和对应的值传递给 remappingFunction 函数。该函数返回的值将作为新的键值对中的值。如果函数返回的值为 null,则会从 Map 中删除该键。

2.当指定的键不存在或其对应的值为 null 时,它会根据 remappingFunction 函数的返回值来确定如何处理:

  • 如果 remappingFunction 返回了一个非 null 的值,则会将新的键值对添加到 Map 中。
  • 如果 remappingFunction 返回了 null,则会从 Map 中删除该键(如果存在)。

示例:

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

public class ComputeExample {
    public static void main(String[] args) {
        // 创建一个 HashMap 实例
        Map<String, Integer> map = new HashMap<>();
        
        // 添加键值对到 HashMap
        map.put("key1", 10);
        map.put("key2", 20);
        map.put("key3", null);

        // 使用 compute 方法更新现有的键值对
        map.compute("key1", (k, v) -> v == null ? 1 : v + 1); // key1 存在,值为 10,将值更新为 11
        map.compute("key2", (k, v) -> v == null ? 1 : v + 1); // key2 存在,值为 20,将值更新为 21
        map.compute("key3", (k, v) -> v == null ? null : v + 1); // key3 存在,值为 null,将值更新为 1
        map.compute("key4", (k, v) -> v == null ? 1 : v + 1); // key4 不存在,直接返回 1
        
        // 输出更新后的 Map 内容
        System.out.println("Updated Map: " + map);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 对于已存在的键值对:
    • 如果值为 null,则将值更新为 1
    • 如果值不为 null,则将原值加 1
  • 对于不存在的键值对或者键的值为null:
    • 对于Key4,remappingFunction 返回了一个非 null 的值,则会将新的键值对添加到 Map 中。
    • 对于Key3,remappingFunction 返回了 null,则会从 Map 中删除该键(如果存在)。

输出更新后的 Map 内容,可以看到每个键值对根据 remappingFunction 的结果进行了更新或未发生改变。:
在这里插入图片描述

Updated Map: {key1=11, key2=21,key4=1}
  • 1

这说明 compute 方法根据指定的函数对 Map 中的键值对进行了更新或操作。


2.computeIfAbsent方法

根据指定的键和计算函数在需要时对指定键的值进行修改。

方法的签名如下:

default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
  • 1
  • key:要操作的键。
  • mappingFunction:一个接收键作为输入,并返回对应值的函数。

computeIfAbsent 方法的行为:

  1. 如果指定的键存在,并且对应的值不为 null,则直接返回该值,不会执行 mappingFunction 函数。
  2. 如果指定的键不存在,或者对应的值为 null,则执行 mappingFunction 函数,将函数返回的结果作为新的键值对中的值,并将其放入 Map

示例:

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

public class ComputeIfAbsentExample {
    public static void main(String[] args) {
        // 创建一个 HashMap 实例
        Map<String, Integer> map = new HashMap<>();
        
        // 添加键值对到 HashMap
        map.put("key1", 10);
        map.put("key2", 20);

        // 使用 computeIfAbsent 方法添加新的键值对
        map.computeIfAbsent("key1", k -> 100); // key1 存在,值为 10,不会执行 mappingFunction
        map.computeIfAbsent("key3", k -> 30);  // key3 不存在,执行 mappingFunction,将值 30 放入 Map
        
        // 输出更新后的 Map 内容
        System.out.println("Updated Map: " + map);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 对于已存在的键值对:
    • 如果值不为 null,则直接返回原值,不会执行 mappingFunction 函数。
  • 对于不存在的键值对(例如 "key3"):
    • 执行 mappingFunction 函数,将函数返回的值作为新的键值对中的值,并将其放入 Map 中。

最后,我们输出更新后的 Map 内容,可以看到只有在键值对不存在时,mappingFunction 函数才会被执行

示例输出:
在这里插入图片描述

Updated Map: {key1=10, key2=20, key3=30}
  • 1

3.computeIfPresent方法

键存在且对应的值不为 null 时对指定键的值进行修改

方法的签名如下:

default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
  • 1
  • key:要操作的键。
  • remappingFunction:一个接收键和当前键对应的值作为输入,并返回一个新值的函数。

computeIfPresent 方法的行为如下:

  1. 如果指定的键存在且对应的值不为 null,则将键和对应的值传递给 remappingFunction 函数。该函数返回的值将作为新的键值对中的值。如果函数返回的值为 null,则会从 Map 中删除该键
  2. 如果指定的键不存在或其对应的值为 null,则 remappingFunction 函数将不会执行,computeIfPresent 方法直接返回 null,不会修改 Map 中的任何内容

示例:

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

public class ComputeIfPresentExample {
    public static void main(String[] args) {
        // 创建一个 HashMap 实例
        Map<String, Integer> map = new HashMap<>();
        
        // 添加键值对到 HashMap
        map.put("key1", 10);
        map.put("key2", 20);

        // 使用 computeIfPresent 方法更新现有的键值对
        map.computeIfPresent("key1", (k, v) -> v * 2); // key1 存在,值为 10,将值更新为 20
        map.computeIfPresent("key2", (k, v) -> v * 2); // key2 存在,值为 20,将值更新为 40
        map.computeIfPresent("key3", (k, v) -> v * 2); // key3 不存在,不会执行 remappingFunction
        
        // 输出更新后的 Map 内容
        System.out.println("Updated Map: " + map);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 对于已存在的键值对:
    • 如果值不为 null,则将键和对应的值传递给 remappingFunction 函数,并将函数返回的值作为新的键值对中的值。
  • 对于不存在的键值对(例如 "key3"):
    • remappingFunction 函数将不会执行,computeIfPresent 方法直接返回 null,不会修改 Map 中的任何内容。

最后,我们输出更新后的 Map 内容,可以看到只有存在且值不为 null 的键值对才会被更新。运行该示例将输出:

在这里插入图片描述

Updated Map: {key1=20, key2=40}
  • 1

computeIfPresent 方法根据指定的函数对 Map存在且值不为 null 的键值对进行了更新或操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/813090
推荐阅读
相关标签
  

闽ICP备14008679号