putIfAbsent、computeIfAbsent、computeIfPresent

2023-12-14 16:36

本文主要是介绍putIfAbsent、computeIfAbsent、computeIfPresent,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

putIfAbsent

判断是否存在,不存在则设置

hashmap.putIfAbsent(K key, V value)

例子如下:

 public static void main(String[] args) {//hashmap.putIfAbsent(K key, V value)HashMap hashMap = Maps.newHashMap();hashMap.put("aa","one");hashMap.putIfAbsent("bb","two");hashMap.values().stream().forEach(e->{System.out.println(e);});hashMap.putIfAbsent("bb","three");hashMap.values().stream().forEach(e->{System.out.println(e);});}

,输出如下:

computeIfAbsent

判断执行的key是否存在,存在则还回对应的value,如果key不存在,则设置进去。

hashmap.computeIfAbsent(K key, Function remappingFunction)

如下例子:

HashMap<String,Integer> hashMap = Maps.newHashMap();hashMap.put("one",280);hashMap.put("two",320);hashMap.put("three",410);Integer aa = hashMap.computeIfAbsent("one",key->290);System.out.println("aa=====:"+aa);System.out.println(hashMap);

输出如下:

computeIfPresent

对hashmap里面的key做判断,存在则对里面的value做重新计算,不存在则还回null

对hashmap里面指定的key的值做重新计算。

hashmap.computeIfPresent(K key, BiFunction remappingFunction)

例子如下:

  HashMap<String,Integer> hashMap = Maps.newHashMap();hashMap.put("one",280);hashMap.put("two",320);hashMap.put("three",410);Integer aa = hashMap.computeIfPresent("four",(key,value)->290);System.out.println("aa=====:"+aa);System.out.println(hashMap);

还回

存在则重新计算如下:还回的是计算过的值

  HashMap<String,Integer> hashMap = Maps.newHashMap();hashMap.put("one",280);hashMap.put("two",320);hashMap.put("three",410);Integer aa = hashMap.computeIfPresent("one",(key,value)->290);System.out.println("aa=====:"+aa);System.out.println(hashMap);

结果如下:

这篇关于putIfAbsent、computeIfAbsent、computeIfPresent的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/493248

相关文章

HashMap putIfAbsent computeIfAbsent 使用方法

方法功能描述: putIfAbsent (a,b) 如果当前map 里面没有key 为a 的数据, 那么把key 为 a,值为b放到map 里面,方法放回null, 如果之前有key 为a 的数据,那么返回a 对应的value,无视参数b computeIfAbsent(a, Function f) 如果map 里面没有key 为a 的数据, 那么使用f 计算一个key对应的value

Java 中Map的Put() 与putIfAbsent() 方法区别

public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.putIfAbsent("A", "1");// get 出来的值会被覆盖map.put("A", "3");// get 出来的值不会被覆盖,如果之前的值是空值,则会被覆盖map.putIfAbsent("A", "2

jdk1.8中map中compute,computeIfAbsent,computeIfPresent方法介绍

1.compute compute:V compute(K key, BiFunction < ? super K, ? super V, ? extends V> remappingFunction) compute的方法,指定的key在map中的值进行操作 不管存不存在,操作完成后保存到map中 HashMap<String,Integer> map = new H

【Java】 getOrDefault, computeIfAbsent, putIfAbsent

getOrDefault, computeIfAbsent, putIfAbsent     这三个方法都很像,都是对map中不存在key时的处理。     这三个函数在执行基于map的分组时会很常用,比如分组求和或者分组生成list。     其中,get的是只读处理,不会影响map的结构。语义是如果不存在返回指定的默认值,否则返回key对应的value。     三者语义上的区别:

HashMap集合中的put()和putIfAbsent()的理解

序言 关于Map集合大家都不陌生,最常用的无非就是HashMap,TreeMap,大家都知道,Map是用于键值对key->value的形式来保存数据的,所以我们最熟悉的方法莫过于map的put方法,map的put方法有好几个,可能会有一些人会搞不懂,我就把自己的对这些put方法的理解记录一下,如果有哪里不对的还请各位大神帮忙提出来,在这我先谢谢各位了,不喜欢,请不要骂我,毕竟我也是自己学习的,下

Jdk1.8,Map新特性,computeIfAbsent,putIfAbsent区别。

个人博客地址:https://grt1228.gitee.io/2020/03/03/Jdk8%20Map%E7%9A%84%E4%B8%80%E4%B8%AA%E6%96%B0%E7%89%B9%E6%80%A7/ Map 1.computeIfAbsent 12 computeIfAbsent方法jdk8新特性,使用map.get("key")时当返回值为null时,我们要给它

Map之computeIfAbsent

Map之computeIfAbsent Absent /ˈæbsənt , æbˈsent/ ab相反s存在ent…的 从map中获取key对应的value,如果value不存在就用提供的Function创建一个新的value,然后存入map,最后返回 优化前 Map<String, Set<Pet>> statistics = new HashMap<>();Set<Pet> pets

[ConcurrentHashMap] 1.computeIfAbsent嵌套使用会造成死循环 2.解决单线程下遍历过程中修改的问题

1)问题1 package org.example.testChm2;import com.google.common.collect.Maps;import java.util.Map;/*** @author jianan* @date 2021/7/2 10:45:06*/public class TestChm2 {public static Map<String, String>

[HashMap] 1.merge 2.compute 3.包装类小坑 4.computeIfAbsent 5.LinkedHashMap 6.removeIf 7.ImmutableMap 8.

1)merge() 我理解这个merge可以废弃了,完全使用conpute代替!!! // 名字其实很直观:合并数量。 不存在时,当然会追加。 // 与compute的区别:merge接收的是3个参数。 而 compute接收的是2个参数 场景:我通过多个途径获取了物品a,发给客户端前需要合并下数量 package org.example.basic;import java

深入理解Java 8 Map.computeIfAbsent方法

Java 8对Map接口进行了一系列的增强,引入了一些非常实用的默认方法。其中,computeIfAbsent方法是一个强大的工具,它可以帮助开发者优化代码,尤其是在处理映射时自动化键的存在性检查和值的懒初始化。这篇文章将带你深入理解computeIfAbsent方法,并通过一个简单的示例来展示它的用法和好处。 什么是 computeIfAbsent? computeIfAbsent 方法是J