java中Map的compute,computeIfAbsent,computeIfPresent的简析

compute(计算)

如果指定的键没有对应的值(没有该键或者该键对应的值是空),
那么使用该键计算新的值,并将其添加到 Map 中

computeIfAbsent(不存在时计算)

computeIfPresent ——如果指定的键在 Map 中存在,就计算该键的新值,并将其添加
到 Map 中

computeIfPresent(存在时计算)

compute ——使用指定的键计算新的值,并将其存储到 Map 中。

相同:返回值是返回最新的值

不相同:compute是有则覆盖,没则添加;computeIfAbsent是有则不操作,没则添加;computeIfPresent是有则覆盖,没值的时候不操作

package com.stpass.upload.handler;

 
import java.util.*; 

public class Main {
 
    public static void main(String[] args) {

        Map map = new HashMap<String, String>();
        Object put = map.put("键", "对应的值");//如果有值则覆盖,没有则添加。如果没值则返回空,有则返回上次的值
        Object put2 = map.put("键", "对应的值2");//如果有值则覆盖,没有则添加。如果没值则返回空,有则返回上次的值
        Object put3 = map.put("键", "对应的值3");//如果有值则覆盖,没有则添加。如果没值则返回空,有则返回上次的值
        System.out.println(put); //输出结果  null
        System.out.println(put2); //输出结果(上次的值)  对应的值
        System.out.println(put3); //输出结果(上次的值)  对应的值2

        //02 这里的操作等价于 01 的操作 k代表键,v代表值
        Map mapCompute = new HashMap<String, String>();
        Object mapComputeValue = mapCompute.compute("键", (k, v) -> "对应的值");//有值则覆盖,没值则添加,这个跟put一样,但是返回是目前的值,put是返回上次的值
        Object mapComputeValue2 = mapCompute.compute("键", (k, v) -> "对应的值2");//有值则覆盖,没值则添加,这个跟put一样,但是返回是目前的值,put是返回上次的值
        System.out.println(mapComputeValue); //输出结果  对应的值
        System.out.println(mapComputeValue2); //输出结果  对应的值2

        //03 如果map中不存在等于"键"的k,则添加为"键"的k以及"对应的值",如果有,则不操作
        System.out.println("--------------mapComputeIfAbsent-----------------");
        Map mapComputeIfAbsent = new HashMap<String, String>();
        Object valueAbsent = mapComputeIfAbsent.computeIfAbsent("键", k -> "value");
        Object valueAbsent2 = mapComputeIfAbsent.computeIfAbsent("键", k -> "value2");
        System.out.println(mapComputeIfAbsent); //输出结果 {键=value}
        System.out.println(valueAbsent); //输出结果 value
        System.out.println(valueAbsent2); //输出结果 value


        //04 如果map中不存在等于"键"的k,则不操作,有才覆盖,跟computeIfAbsent相反
        System.out.println("--------------computeIfPresent-----------------");
        Map mapIfPresent = new HashMap<String, String>();
        Object valuePresent = mapIfPresent.computeIfPresent("键", (k,v) -> "value");
        System.out.println(mapIfPresent); //输出结果 {}
        System.out.println(valuePresent); //输出结果 null
        mapIfPresent.put("键", "对应的值");
        Object valuePresent2 = mapIfPresent.computeIfPresent("键",(k,v) -> "value2");
        System.out.println(mapIfPresent); //输出结果 {键=value2}
        System.out.println(valuePresent2); //输出结果 value2


        System.out.println(mapIfPresent.remove("键","对应的值"));
        System.out.println(mapIfPresent.remove("键","value2"));
        System.out.println("------------------getOrDefault----------------------------");
        Map getOrdefault = new HashMap<String, String>();
        Object orDefault = getOrdefault.getOrDefault("java", "ifconfig");//这里ifconfig是一个默认值,不会添加到Map里面去
        System.out.println(orDefault);
        System.out.println(getOrdefault);

        System.out.println("------------------replaceAll----------------------------");
        Map<String, String> favouriteMovies = new HashMap<>();
        favouriteMovies.put("Raphael", "Star Wars");
        favouriteMovies.put("Olivia", "james bond");
        favouriteMovies.replaceAll((friend, movie) -> {return friend.toLowerCase();});
        System.out.println(favouriteMovies);//输出 {Olivia=olivia, Raphael=raphael}

        System.out.println("------------------replace----------------------------");
        Map<String, String> favouriteMovies2 = new HashMap<>();
        favouriteMovies2.put("Raphael", "Star Wars");
        favouriteMovies2.put("Olivia", "james bond");
        favouriteMovies2.replace("Raphael","bond");
        System.out.println(favouriteMovies2);;//输出 {Olivia=james bond, Raphael=bond}


        System.out.println("------------------merge----------------------------");
        Map<String, String> m1 = new HashMap<>();
        m1.put("Raphael", "Star Wars");
        m1.put("Olivia2", "james bond2");
        m1.put("Olivia", "james bon2d");
        Map<String, String> m2 = new HashMap<>();
        m2.put("Raphael", "Star Wars");
        m2.put("Olivia", "james bond");

        Map<String, String> everyone = new HashMap<>(m1);
        everyone.putAll(m2);
        System.out.println(everyone);//{Olivia=james bond, Raphael=Star Wars, Olivia2=james bond2}

        Map<String, String> everyone2 = new HashMap<>(m1);

        m2.forEach((k,v)->everyone2.merge(k,v,(k1,k2)->k1+"&"+k2));//{Olivia=james bon2d&james bond, Raphael=Star Wars&Star Wars, Olivia2=james bond2}
        m2.forEach((k,v)->everyone2.merge(k,v,(k1,k2)->k1==k2?k1:k1+"&"+k2));//{Olivia=james bon2d&james bond, Raphael=Star Wars, Olivia2=james bond2}
        System.out.println(everyone2);



    }


}

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值