Map中的computeIfAbsent 方法

news/2024/10/18 14:24:29/

说明

computeIfAbsent 方法是 Map 接口中的一个方法,它可以用于在执行某个键的映射值不存在时,通过提供的函数计算并将其放入到 Map 中。相比于传统的 if-else 语句,使用 computeIfAbsent 可以提供更简洁和优雅的代码。

使用 computeIfAbsent 可以避免显式的 if-else 逻辑,通过传递一个 lambda 函数来自动处理键不存在的情况。这种方式允许我们将逻辑集中在一个地方,提高代码的可读性和可维护性。

以下是一个使用 computeIfAbsent 替代 if-else 的示例,演示了在 Map 中根据键获取或计算映射值的情况:

import java.util.HashMap;
import java.util.Map;public class ComputeIfAbsentExample {public static void main(String[] args) {Map<String, Integer> map = new HashMap<>();// 传统的 if-else 方式String key = "foo";if (map.containsKey(key)) {int value = map.get(key);// 处理映射值存在的逻辑System.out.println("Value exists: " + value);} else {// 处理映射值不存在的逻辑int newValue = computeValue(key);map.put(key, newValue);System.out.println("New value computed and added to map: " + newValue);}// 使用 computeIfAbsent 方式map.computeIfAbsent(key, k -> computeValue(k));int value = map.get(key);System.out.println("Final value: " + value);}private static int computeValue(String key) {// 模拟计算映射值的过程System.out.println("Computing value for key: " + key);return key.length();}
}

在这个示例中,我们使用了一个 Map 对象存储键值对。首先,我们展示了传统的 if-else 方式来检查键是否存在,然后根据情况执行相应的逻辑。接下来,我们使用 computeIfAbsent 方法来简化该逻辑,只需提供计算映射值的函数即可。如果键不存在,该函数将被调用,并将计算得到的映射值放入到 Map 中。

通过使用 computeIfAbsent,我们可以减少代码量并提高代码清晰度。这个方法更适合在我们需要根据键的存在与否执行不同的逻辑时使用。

请注意,这只是一个示例,实际使用时,你需要根据具体的需求和数据结构适当应用 computeIfAbsent 方法。

Simply put

The computeIfAbsent method in Java’s Map interface allows you to compute a value for a given key if it is not already associated with a value in the map. Here’s how it works:

  1. The method takes two parameters: the key for which you want to compute a value, and a Function that specifies how to compute the value.
  2. It checks if the key is present in the map. If it is, the method returns the associated value.
  3. If the key is not present, the Function is invoked with the key as the input. The Function calculates and returns the value based on the key.
  4. The returned value is then associated with the key in the map.
  5. Finally, the method returns the computed value.

Here’s an example to illustrate the usage of computeIfAbsent :

import java.util.HashMap;
import java.util.Map;public class MapExample {public static void main(String[] args) {Map<String, Integer> map = new HashMap<>();// Compute and associate a value for the key "key" if it is absentInteger value = map.computeIfAbsent("key", k -> k.length());System.out.println("Value: " + value); // Output: Value: 3System.out.println("Map: " + map); // Output: Map: {key=3}}
}

In the above example, we create an empty HashMap and use the computeIfAbsent method to compute and associate a value for the key “key”. Since the key is not present in the map, the Function calculates the length of the key and returns it. The computed value (3) is associated with the key “key” in the map. Finally, we print the computed value and the updated map.


http://www.ppmy.cn/news/742949.html

相关文章

TI-Nspire CX CAS图形计算器模拟器+操作指南

TI Nspire 系列计算器是当今最为强大的计算器之一。它以其卓越的性能、丰富的功能独树一帜&#xff0c;产品定位教育领域&#xff0c;非科研、工程。其程序语言简单&#xff0c;界面友好易用&#xff0c;编程中也支持自然书写显示&#xff08;也就是不会在编程中使用线性格式&a…

TI CCS9 C6000等系列库函数哪里找?

最近公司有个项目&#xff0c;要用到TI 公司的TMS320 C6655这款芯片做数据处理。因为我之前用过七八年的DSP&#xff08;是AD公司的&#xff09;&#xff0c;所以这份任务就分给我了。 我上网大概看了看&#xff0c;感觉用DSP的人真的是越来越少了&#xff0c;大部分人现在都在…

凯恩帝k1000ti参数设置_KND1000TI系统参数 - 图文 -

宝鸡机床厂SK40/50P配KND1000TI系统参数设置 (PLC版本&#xff1a;k1000tii_baojisp4_3.0 &#xff1b;软件版本&#xff1a;1000TD_ A04i_110411) 参数号 001 002 003 004 005 006 007 参数值 10011011 11111010 01110001 11000000 第5位设置为1表示在返回参考点时减速信号为\…

Xiaoije雷达之路---介绍DBF测角方法及TI的实现方法

Hello,大家好,我是Xiaojie,欢迎大家能够和Xiaojie来一起学习毫米波雷达知识,本篇文章主要是介绍一下一种测角的方法DBF以及该方法在TI毫米波雷达设备中是如何实现的,一起来看看吧!!! 本篇文章主要从几个模块进行讲解: 数字波束形成理论介绍MATLAB程序仿真TI中BF源码讲解…

TI-RTOS之初体验(1)

自从学单片机以来&#xff0c;陆续接触了一些嵌入式操作系统&#xff0c;从最开始的51单片机&#xff0c;叫做rtx51_tiny&#xff0c;就是在keil C51安装后&#xff0c;例程里边自带一个实时操作系统例程&#xff0c;可以用它来建立多个任务&#xff08;函数&#xff09;同时执…

字段缩写ti表示什么_EBSCOhost数据库中,检索字段代码为TI、SO、AU分别表示

【单选题】20. 计算 vt. 【单选题】以下哪个数据库更适合查找学术类医学护理全文,比如利用医学主题词查找,解决您毕业论文写作中需要引用外文参考文献的困难? 【单选题】A202下列选项中,不属于Java关键字的是() 【单选题】A555Java中,隐藏信息的基本机制是() 【多选题】用户在…

TI快充芯片-BQ25890

它是一个针对单个锂电池和聚合物锂电池的&#xff0c;5A开关模式电池充电芯片。它集成输入反转模块FET&#xff08;RBFET-Q1&#xff09;&#xff0c;高端开关FET&#xff08;HSFET-Q2&#xff09;&#xff0c;低端开关FET&#xff08;LSFET-Q3&#xff09;和电池FET&#xff0…

FOC中的PARK变换_TI和ST电机控制库的源码实现

FOC中的Clarke变换_TI和ST电机控制库的源码实现 FOC中的PARK变换_TI和ST电机控制库的源码实现 FOC中的反PARK变换_TI和ST电机控制库的源码实现 park变换 该变换将平衡两相正交平稳系统中的矢量变换为正交旋转坐标系。 数学公式&#xff1a; { I D I α cos ⁡ θ I β s…