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不为空的属性