jdk1.8中map新特性compute,computeIfAbsent,computeIfPresent,putIfAbsent方法

jdk1.8中map新特性compute,computeIfAbsent,computeIfPresent,putIfAbsent方法

1.compute
compute:key不存在添加,存在则对value进行操作

Map<String, Integer> map = Maps.newHashMap();
            map.put("1", 1);
            map.put("2", 2);
            map.put("3", 3);
            map.put("4", 4);
        Integer integer = map.compute("4", (k, v) -> v+1);
        Integer integer1 = map.compute("5", (k, v) -> {
            if (v == null) {
                return 0;
            }
            return v+1;
        });

        System.out.println(integer);   //输出 5
        System.out.println(integer1);   //输出 0
        System.out.println(map.toString()); //输出 {1=1, 2=2, 3=3, 4=5, 5=0}

2.computeIfAbsent
computeIfAbsent:当key存在返回当前value值,不存在则添加

Map<String, Integer> map = Maps.newHashMap();
        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 3);
        map.put("4", 4);

        Integer integer = map.computeIfAbsent("4", k -> 5);
        //key不存在就添加
        Integer integer1 = map.computeIfAbsent("5", k -> {
            if (k.equals("5")){
                return 50;
            }
            return 0;
        });

        System.out.println(integer);        //输出 4
        System.out.println(integer1);       //输出 50
        System.out.println(map.toString()); //输出 {1=1, 2=2, 3=3, 4=4, 5=50}

3.computeIfPresent
computeIfPresent :对在map中已有key的value进行操作。

Map<String, Integer> map = Maps.newHashMap();
            map.put("1", 1);
            map.put("2", 2);
            map.put("3", 3);
            map.put("4", 4);

        //对集合中存在的key进行操作
        Integer integer = map.computeIfPresent("4", (k, v) -> v+1);
        Integer integer1 = map.computeIfPresent("5", (k, v) -> {
            if (v == null) {
                return 0;
            }
            return v+1;
        });

        System.out.println(integer);   //输出 5
        System.out.println(integer1);   //输出 null
        System.out.println(map.toString()); //输出 {1=1, 2=2, 3=3, 4=5}

4.putIfAbsent
putIfAbsent:map集合中key不存在 或 value为空时添加,如果key已经存在,则不进行操作,返回value。

Map<String, Integer> map = Maps.newHashMap();
            map.put("1", 1);
            map.put("2", 2);
            map.put("3", 3);
            map.put("4", 4);
            map.put("5", null);

        Integer integer = map.putIfAbsent("4", 5);
        Integer integer1 = map.putIfAbsent("5", 0);
        //等价于
            /*if(!map.containsKey("5") || map.get("5") == null) {
                map.put("5", 0);
            }*/

        System.out.println(integer);   //输出 4
        System.out.println(integer1);   //输出 0
        System.out.println(map.toString()); //输出 {1=1, 2=2, 3=3, 4=4, 5=0}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值