java8 Map中新增的方法介绍

相信大家都有看到,在 java8 后,大量的方法都包含两个单词,ifAbsent 和 ifPresent。
absent 表示缺席,理解为当前不存在 即 ifAbsent 在map中主要意思就是 如果 get(key) == null 执行
present 表示当下,理解为当下有值, 即 ifPresent 在map中主要的意思就是 如果 get(key) != null 执行

这里介绍常用的一些方法:

putIfAbsent(K key, V value) 如果传入key对应的value已经存在,就返回存在的value,不进行替换。
如果不存在或为 get(key)为null,就添加key和value,返回null(简单讲就是返回前一个值)


    /**
     *  putIfAbsent(K key, V value)   如果传入key对应的value已经存在,就返回存在的value,不进行替换。
     *      如果不存在或为 get(key)为null,就添加key和value,返回null(简单讲就是返回前一个值)
     */
    public static void testPutIfAbsent(){
        String ret;
        Map<String, String> map = new HashMap<>();
        ret = map.putIfAbsent("a", "aaa");
//        null {a=aaa}
        System.out.println(ret + " " + map);

        ret = map.putIfAbsent("a", "bbb");
//        aaa {a=aaa}
        System.out.println(ret + " " + map);

        map.put("b", null);
        ret = map.putIfAbsent("b", "bbb");
//        null {a=aaa, b=bbb}
        System.out.println(ret + " " + map);
    }

computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
如果传入的 key 不存在或 get(key)为空,则返回空,不处理。
如果 get(key) 不为空则执行 BiFunction.apply(key,oldValue),
如果执行结果为 null 则删除 key,执行结果不为null 则返回执行结果


    /**
     *   computeIfPresent(K key,
     *             BiFunction<? super K, ? super V, ? extends V> remappingFunction)
     *      如果传入的 key 不存在或 get(key)为空,则返回空,不处理;
     *      如果 get(key) 不为空则执行 BiFunction.apply(key,oldValue), 如果执行结果为 null 则删除 key,
     *      执行结果不为null 则返回执行结果
     */
    public static void testComputeIfPresent(){
        String ret;
        Map<String, String> map = new HashMap<>();
        ret = map.computeIfPresent("a", (key, value) -> key + value);
//        null {}
        System.out.println(ret + " " + map);

        map.put("a", null);
        ret = map.computeIfPresent("a", (key, value) -> key + value);
//        null {a=null}
        System.out.println(ret + " " + map);

        map.put("a", "+aaa");
        ret = map.computeIfPresent("a", (key, value) -> key + value);
//        a+aaa {a=a+aaa}
        System.out.println(ret + " " + map);
        ret = map.computeIfPresent("a", (key, value) -> null);
//        null {}
        System.out.println(ret + " " + map);
    }

computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
如果传入的 key 值不存在,或 get(key) 为 null, 则执行 Function 结果并设置为新值
如果传入的 key 值存在,且 get(key)不为 null, 则忽略函数,直接返回当前值


    /**
     *   computeIfAbsent(K key,
     *             Function<? super K, ? extends V> mappingFunction)
     *      如果传入的 key 值不存在,或 get(key) 为 null, 则执行 Function 结果并设置为新值
     *      如果传入的 key 值存在,且 get(key)不为 null, 则忽略函数,直接返回当前值
     *
     */
    public static void testComputeIfAbsent(){
        String ret;
        Map<String, String> map = new HashMap<>();
        ret = map.computeIfAbsent("a", key -> key + "123");
//        a123 {a=a123}
        System.out.println(ret + " " + map);

        ret = map.computeIfAbsent("a", key -> key + "456");
//        a123 {a=a123}
        System.out.println(ret + " " + map);

        map.put("a", null);
        ret = map.computeIfAbsent("a", key -> key + "456");
//        a123 {a=a123}
        System.out.println(ret + " " + map);

        ret = map.computeIfAbsent("a", key -> null);
//        a123 {a=a123}
        System.out.println(ret + " " + map);
    }

replace(K key, V value)
如果传入的 key 存在(无论 get(key) 是否为null),则 替换值,返回 旧值
如果不存在 key ,则 不处理

/**
     *  replace(K key, V value)
     *      如果传入的 key 存在(无论 get(key) 是否为null),则 替换值,返回 旧值
     *      如果不存在 key ,则 不处理
     */
    public static void testReplace(){
        String ret;
        Map<String, String> map = new HashMap<>();
        ret = map.replace("a", "abc");
//        null {}
        System.out.println(ret + " " + map);

        map.put("a", "ddd");
        ret = map.replace("a", "abc");
//        ddd {a=abc}
        System.out.println(ret + " " + map);

        ret = map.replace("a", null);
//        abc {a=null}
        System.out.println(ret + " " + map);
    }

boolean replace(K key, V oldValue, V newValue)
如果传入的 key 不存在,则返回 false,
如果传入的 key 存在,且指定的 oldValue = get(key) 采用 newValue 替代旧值,返回 ture
如果传入的 key 存在,但指定的 oldValue != get(key) 则不处理,返回 false

  /**
     *  boolean replace(K key, V oldValue, V newValue)
     *      如果传入的 key 不存在,则返回 false,
     *      如果传入的 key 存在,且指定的 oldValue = get(key) 采用 newValue 替代旧值,返回 ture
     *      如果传入的 key 存在,但指定的 oldValue != get(key) 则不处理,返回 false
     */
    public static void testReplace2(){
        boolean ret;
        Map<String, String> map = new HashMap<>() ;
        ret = map.replace("a", null, "aaa");
//        false {}
        System.out.println(ret + " " + map);

        map.put("a", null);
        ret = map.replace("a", null, "aaa");
//        true {a=aaa}
        System.out.println(ret + " " + map);

        ret = map.replace("a", "aaa", null);
//        true {a=null}
        System.out.println(ret + " " + map);

        ret = map.replace("a", "aaa", "bbb");
//        false {a=null}
        System.out.println(ret + " " + map);
    }

replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
对 Map 中的每一个元素应用函数 Bifunction, 新值为 Bifunction.apply(k,v) 的返回值

 /**
     *  replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
     *      对 Map 中的每一个元素应用函数 Bifunction, 新值为 Bifunction.apply(k,v) 的返回值
     */
    public static void testReplaceAll(){
        Map<String, String> map = new HashMap<>() ;
        map.put("a", "aaa");
        map.put("b", "bbb");
        map.put("c", null);
//        {a=aaa, b=bbb, c=null}
        System.out.println(map);
        map.replaceAll((key, value) -> key + "-" + value);
//        {a=a-aaa, b=b-bbb, c=c-null}
        System.out.println(map);
    }

V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
无论 key 是否存在,get(key) 是否为null, 都执行 BiFunciton , 但 key 不存在时,默认 get(key) 为 null;
BiFunction 执行结果作为 key 的新值,但 BiFunction 返回 null时, 则是删除该 key , 返回null,
其他情况就返回结果为新值

compute 方法是 testComputeIfAbsent 和 testComputeIfPresent 的结合体
无论 key 是否为空都执行,结果集为 null 则删除key

/**
     * V compute(K key,
     *             BiFunction<? super K, ? super V, ? extends V> remappingFunction)
     *      无论 key 是否存在,get(key) 是否为null, 都执行 BiFunciton , 但 key 不存在时,默认 get(key) 为 null
     *      BiFunction 执行结果作为 key 的新值,但 BiFunction 返回 null时, 则是删除该 key
     *      返回结果为新的值
     *
     *      compute 方法是 testComputeIfAbsent 和 testComputeIfPresent 的结合体,
     *          无论 key 是否为空都执行,结果集为 null 则删除key
     */
    public static void testCompute(){
        String ret;
        Map<String, String> map = new HashMap<>() ;
        ret = map.compute("a", (key, value) -> "a" + value);
//        anull {a=anull}
        System.out.println(ret + " " + map);
        ret = map.compute("a", (key, value) -> "a" + value);
//        aanull {a=aanull}
        System.out.println(ret + " " + map);
        ret = map.compute("a", (key, value) -> null);
//        null {}
        System.out.println(ret + " " + map);
    }

merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
如果指定的 key 不存在,或 get(key) 为 null 时,则将 value 作为新值
如果get(key) 不为null 则执行 BiFunction.apply(oldValue,value) 作为新值,
如果新值为 null, 则删除该 key.返回 null,
如果新值不为 null, 则返回

/**
     * merge(K key, V value,
     *             BiFunction<? super V, ? super V, ? extends V> remappingFunction)
     *      如果指定的 key 不存在,或 get(key) 为 null 时,则将 value 作为新值
     *      如果get(key) 不为null 则执行 BiFunction.apply(oldValue,value) 作为新值,
     *      如果新值为 null, 则删除该 key.返回 null,
     *      如果新值不为 null, 则返回
     */
    public static void testMerge(){
        String ret;
        Map<String, String> map = new HashMap<>() ;
        ret = map.merge("a", "aa", (oldValue, value) -> oldValue + "-" + value);
//        aa {a=aa}
        System.out.println(ret + " " + map);

        ret = map.merge("a", "bb", (oldValue, value) -> oldValue + "-" + value);
        System.out.println(ret + " " + map);

        ret = map.merge("a", "bb", (oldValue, value) -> null);
        System.out.println(ret + " " + map);

        map.put("a", null);
        ret = map.merge("a", "aa", (oldValue, value) -> oldValue + "-" + value);
        System.out.println(ret + " " + map);

        map.put("a", null);
        ret = map.merge("a", "bb", (oldValue, value) -> null);
        System.out.println(ret + " " + map);

        ret = map.merge("a", null, (oldValue, value) -> oldValue + "-" + value);
        System.out.println(ret + " " + map);
    }
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值