JDK1.8中Map的getOrDefault、putIfAbsent、computeIfAbsent、merge方法

一、getOrDefault

getOrDefault获取当前key的value值,如果Map中存在这个key,就获取到key对应的value值,如果不存在key,就返回一个传入的默认值,常用于往集合。
源码:

    default V getOrDefault(Object key, V defaultValue) {
        V v;
        //如果key对应得value不为空,或者Map包含了key,则取得value得值,否则得到默认值。
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }

例子:

    public static void main(String[] args) {
        String s="tcc";
        HashMap<Character, Integer> map = new HashMap<>();
        //将字符串的字符放入map中,并统计字符出现次数
        for (int i=0;i<s.length();i++){
            map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
        }
        System.out.println(map);
    }

输出:

    {c=2, t=1}

二、putIfAbsent;

putIfAbsent添加键值对,如果map集合中没有该key对应的值,则直接添加,如果已经存在对应的值,则依旧为原来的值。

与put对比:
put添加键值对,如果map集合中没有该key对应的值,则直接添加,如果已经存在对应的值,则覆盖旧值。

源码:

    default V putIfAbsent(K key, V value) {
        V v = get(key);
        //如果键为空,则添加
        if (v == null) {
            v = put(key, value);
        }
        //否则跳过该值不处理
        return v;
    }

示例:

    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"tcc");
        map.putIfAbsent(2,"tcc2");
        System.out.println(map);
        //key为1存在,不会覆盖value值
        map.putIfAbsent(1,"tccIf");
        System.out.println(map);
        //key为1存在,会覆盖value值
        map.put(1,"tccPut");
        System.out.println(map);
    }

输出:

    {1=tcc, 2=tcc2}
    {1=tcc, 2=tcc2}
    {1=tccPut, 2=tcc2}

三、computeIfAbsent

computeIfAbsent与putIfAbsent功能基本相似,区别是:

  1. 如果传入的value为null时,computeIfAbsent不会进行put操作而putIfAbsent会进行put。
  2. value的参数不同,computeIfAbsent方法提供了value的计算函数,如果value计算耗时时,可以使用computeIfAbsent节约时间。

源码:

    default V computeIfAbsent(K key,
            Function<? super K, ? extends V> mappingFunction) {
        Objects.requireNonNull(mappingFunction);
        V v;
        //如果key对应得value为空
        if ((v = get(key)) == null) {
            V newValue;
            //如果传入的value不为空则put
            if ((newValue = mappingFunction.apply(key)) != null) {
                put(key, newValue);
                return newValue;
            }
        }

        return v;
    }

示例:

    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.computeIfAbsent(1, v -> "tcc");
        //key存在,不会覆盖
        map.computeIfAbsent(1, v -> "tccIf");
        System.out.println(map);
        //value为空,不会put
        map.computeIfAbsent(2, v -> null);
        System.out.println(map);
    }

输出:

    {1=tcc}
    {1=tcc}

四、merge

merge添加键值对,如果key对应的value不存在,直接存储value值,如果value值存在,更新value值,我们可以自定义更新规则。

  1. 使用新value覆盖旧的value: (value1, value2) -> value2
  2. 保留旧的value:(value1, value2) -> value1
  3. 保存新value+旧value,例如:(value1, value2) -> value1+ value2

源码:

	//BiFunction函数式接口,提供apply方法
    default V merge(K key, V value,
            BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        Objects.requireNonNull(value);
        V oldValue = get(key);
        //经过函数修改后的value
        V newValue = (oldValue == null) ? value :
                   remappingFunction.apply(oldValue, value);
        //计算出来的value为空,删除这个key
        if(newValue == null) {
            remove(key);
        } else {
        //存入修改后的value
            put(key, newValue);
        }
        return newValue;
    }

案例:

    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(1,2);
        map.merge(1,5,(v1,v2)->v1+v2);
        map.merge(2,10,(v1,v2)->v2);
        System.out.println(map);

    }

输出:

  {1=7, 2=10}

猜你喜欢

转载自blog.csdn.net/tc979907461/article/details/106082522
今日推荐