java8 map新增方法使用

java8 map新增方法

putIfAbsent(K key, V value)使用

putIfAbsent(K key, V value)表示如果key存在则方法返回value值,如果key不存在则将key-value插入map集合中。示例如下:

        Map<String,Object> map=new HashMap<>();
        map.put("name","tom");
        Object o = map.putIfAbsent("name", "李四");
        System.out.println("putIfAbsent方法返回值:"+o);
        System.out.println("map集合中的值:"+map);
        
		putIfAbsent方法返回值:tom
		map集合中的值:{name=tom}

由于map中存在key为name的属性,所以putIfAbsent方法返回了name对应的value值,并未做新增以及其他操作,如果key不存在map中,则map执行新增操作。示例如下:

        Map<String,Object> map=new HashMap<>();
        map.put("name","tom");
        Object o = map.putIfAbsent("age", 24);
        System.out.println("putIfAbsent方法返回值:"+o);
        System.out.println("map集合中的值:"+map);

		putIfAbsent方法返回值:null
		map集合中的值:{name=tom, age=24}

map中不存在key为age的属性,则map进行了新增操作。
putIfAbsent方法的作用是:map中不存在key就新增。

compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)使用

compute方法有3中使用方式:

  • 如果key存在,且value不为空,则更新map中对应key的value值
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.compute("name", (key, value) -> "杰克");
        System.out.println("compute方法返回值:"+o);
        System.out.println("map集合中的值:"+map);
		
		compute方法返回值:杰克
		map集合中的值:{name=杰克}
  • 如果key存在,且value为空,则删除map中的key
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.compute("name", (key, value) -> null);
        System.out.println("compute方法返回值:"+o);
        System.out.println("map集合中的值:"+map);
        
        compute方法返回值:null
		map集合中的值:{}
  • 如果key不存在,则将key-value新增至map中
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.compute("age", (key, value) -> 14);
        System.out.println("compute方法返回值:"+o);
        System.out.println("map集合中的值:"+map);
        
		compute方法返回值:14
		map集合中的值:{name=tom, age=14}

compute作用是map中key存在就更新,map中key不存在就新增

computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)使用

computeIfAbsent有如下几种使用方式:

  • 如果map中存在key,且value不为空,则返回value值
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.computeIfAbsent("name", value -> "李四");
        System.out.println("computeIfAbsent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfAbsent方法返回值:tom
		map集合中的值:{name=tom}
  • 如果map中存在key,且value为null,则更新value值
        Map<String, Object> map = new HashMap<>();
        map.put("name", null);
        Object o = map.computeIfAbsent("name", value -> "李四");
        System.out.println("computeIfAbsent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfAbsent方法返回值:李四
		map集合中的值:{name=李四}
  • 如果map中不存在key,且传入参数value不为空,则将key-value新增值map中
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.computeIfAbsent("age", value -> 18);
        System.out.println("computeIfAbsent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfAbsent方法返回值:18
		map集合中的值:{name=tom, age=18}
  • 如果map中不存在key,且传入参数value为空,则什么也不做
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.computeIfAbsent("age", value -> null);
        System.out.println("computeIfAbsent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfAbsent方法返回值:null
		map集合中的值:{name=tom}

computeIfAbsent作用是更新map中value为null的key,map中不存在key就新增

computeIfPresent(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)使用

computeIfPresent有如下几种使用方式:

  • 如果map中key存在,且value不为空,则更新map中key对应的value(传入参数value不为空)
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.computeIfPresent("name", (key,value) -> "李四");
        System.out.println("computeIfPresent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfAbsent方法返回值:李四
		map集合中的值:{name=李四}
  • 如果map中key存在,且value不为空,且传入参数value为空,则删除map中的key
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.computeIfPresent("name", (key,value) -> null);
        System.out.println("computeIfPresent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfPresent方法返回值:null
		map集合中的值:{}
  • 如果map中key存在,且value为空,则直接返回空,不做其他事
        Map<String, Object> map = new HashMap<>();
        map.put("name", null);
        Object o = map.computeIfPresent("name", (key,value) -> "李四");
        System.out.println("computeIfPresent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfPresent方法返回值:null
		map集合中的值:{name=null}
  • 如果map中key不存在,则什么也不做
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        Object o = map.computeIfPresent("age", (key,value) -> 18);
        System.out.println("computeIfPresent方法返回值:" + o);
        System.out.println("map集合中的值:" + map);

		computeIfPresent方法返回值:null
		map集合中的值:{name=tom}

computeIfPresent作用是更新map中的value不为空的属性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值