java 8 hashMap新增方法

2017-11-29  本文已影响0人  吃花生的小猴子

replace

把newValue替换oldValue.

   @Override
    public boolean replace(K key, V oldValue, V newValue) {
        Node<K,V> e; V v;
        if ((e = getNode(hash(key), key)) != null &&
            ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
            e.value = newValue;
            afterNodeAccess(e);
            return true;
        }
        return false;
    }

    @Override
    public V replace(K key, V value) {
        Node<K,V> e;
        if ((e = getNode(hash(key), key)) != null) {
            V oldValue = e.value;
            e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
        return null;
    }

getOrDefault (Object key, V defaultValue)

根据key获取value, 如果value为空则返回defaultValue

   @Override
    public V getOrDefault(Object key, V defaultValue) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
    }

remove(Object key, Object value)

根据key和value 删除

   @Override
    public boolean remove(Object key, Object value) {
        return removeNode(hash(key), key, value, true, true) != null;
    }

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

映射当key不存在或value为null时的value值,包括创建对应key,返回value,如果key存在,则不做处理,例:

        
        Map<String ,Integer> map = new HashMap<>();
        map.put("a",1);

        int result = map.computeIfAbsent("a1", (v) -> 90);

        System.out.println(map.get("a1"));
        System.out.println(result);

        int av = map.computeIfAbsent("a", (v) -> 90);
        System.out.println(av);

      结果:
            90
            90
            1

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

与上述的computeIfAbsent 相反,当key存在时计算替换相应的value,并返回。当key不存在时报NPE,例:

        Map<String ,Integer> map = new HashMap<>();
        map.put("a",1);
        
        int aValue = map.computeIfPresent("a" ,(k,v) -> 21 + v);
        System.out.println(aValue);

      结果:
           21

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

也是计算key的value,如果key不存在,则创建key并复制value,如果value为null,则报NPE,例:

        Map<String ,Integer> map = new HashMap<>();
        map.put("a",1);

        int result = map.compute("a1", (k,v) -> 91);

        System.out.println(map.get("a1"));
        System.out.println(result);

        int av = map.compute("a", (k,v) -> 92);
        System.out.println(av);

        int anull = map.compute("a", (k,v) -> null);
        System.out.println(anull);

如果:
   91
   91
   92
Exception in thread "main" java.lang.NullPointerException

merge(K key, V value,BiFunction<? super V, ? super V, ? extends V> remappingFunction)

合并map

        Map<String ,Integer> map = new HashMap<>();
        map.put("a",1);

        map.merge("a", 21,(a,b) -> a + b);
        System.out.println(map.get("a"));


        map.merge("a1", 22,(a,b) -> a + b);
        System.out.println(map.get("a1"));


        map.merge("a2", null,(a,b) -> a + b);
        System.out.println(map.get("a2"));

结果:
      22
      22
Exception in thread "main" java.lang.NullPointerException

上一篇下一篇

猜你喜欢

热点阅读