15java源码解析-map接口

其他 源码解析 https://blog.csdn.net/qq_32726809/article/category/8035214

前言

这里面会有一个关键字 default

  • 一般来说,接口是没有方法体的,但是用default来修饰的话,便可以有方法体,而且,实现这个接口也不用实现这个方法。

需要实现的方法

int size();

返回键值对的数量

boolean isEmpty();

如果空,返回true

containsKey(Object key);

如果map中包含指定的key,则返回true

containsValue(Object value);

如果值中有对应的对象返回true

keySet()

返回一个所有key的set集合

entrySet()

返回一个 内部类的set集合,这个集合中包含key-value键值对

内部接口

  interface Entry<K,V> {
	       
	        V getValue();

	      
	        V setValue(V value);

	    
	        boolean equals(Object o);

	       
	        int hashCode();

	      
	        public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
	            return (Comparator<Map.Entry<K, V>> & Serializable)
	                (c1, c2) -> c1.getKey().compareTo(c2.getKey());
	        }

	       
	        public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
	            return (Comparator<Map.Entry<K, V>> & Serializable)
	                (c1, c2) -> c1.getValue().compareTo(c2.getValue());
	        }

	       
	        public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
	            Objects.requireNonNull(cmp);
	            return (Comparator<Map.Entry<K, V>> & Serializable)
	                (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
	        }

	      
	        public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
	            Objects.requireNonNull(cmp);
	            return (Comparator<Map.Entry<K, V>> & Serializable)
	                (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
	        }
	    }

默认方法

getOrDefault

获取指定键所对应的值

  default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }
  • 如果没有这个值,就返回默认值

forEach

循环对每个键值对执行操作,action就是要执行的操作

  default void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);/*-------1*/
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {
                k = entry.getKey();
                v = entry.getValue();
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
            action.accept(k, v);/*-------2*/
        }
    }
  • 1处的意思是对对象进行判断,如果为空,抛出异常
  • 2处的是调用要处理的操作

replaceAll

将 function.apply(k, v)获取的值替换

 default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
        Objects.requireNonNull(function);
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {
                k = entry.getKey();
                v = entry.getValue();
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }

            // ise thrown from function is not a cme.
            v = function.apply(k, v);

            try {
                entry.setValue(v);
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
        }
    }

putIfAbsent

如果对应的key没有值,则将value赋给这个key

 default V putIfAbsent(K key, V value) {
        V v = get(key);
        if (v == null) {
            v = put(key, value);
        }

        return v;
    }

replace

单独替换

  • key 键值
  • oldValue 原期望值
  • newValue 要替换的值
default boolean replace(K key, V oldValue, V newValue) {
        Object curValue = get(key);
        if (!Objects.equals(curValue, oldValue) ||
            (curValue == null && !containsKey(key))) {
            return false;
        }
        put(key, newValue);
        return true;
    }

replace

 default V replace(K key, V value) {
        V curValue;
        if (((curValue = get(key)) != null) || containsKey(key)) {
            curValue = put(key, value);
        }
        return curValue;
    }

computeIfAbsent

如果缺少值,则通过函数计算出值

  • key 键
  • mappingFunction 映射函数,通过key计算出值放到map里
default V computeIfAbsent(K key,
            Function<? super K, ? extends V> mappingFunction) {
        Objects.requireNonNull(mappingFunction);
        V v;
        if ((v = get(key)) == null) {
            V newValue;
            if ((newValue = mappingFunction.apply(key)) != null) {
                put(key, newValue);
                return newValue;
            }
        }

        return v;
    }

computeIfPresent

计算现在值,把老的值替换掉

 default V computeIfPresent(K key,
            BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        V oldValue;
        if ((oldValue = get(key)) != null) {
            V newValue = remappingFunction.apply(key, oldValue);
            if (newValue != null) {
                put(key, newValue);
                return newValue;
            } else {
                remove(key);
                return null;
            }
        } else {
            return null;
        }
    }

compute

计算键的应设值

default V compute(K key,
            BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        V oldValue = get(key);

        V newValue = remappingFunction.apply(key, oldValue);
        if (newValue == null) {
            // delete mapping
            if (oldValue != null || containsKey(key)) {
                // something to remove
                remove(key);
                return null;
            } else {
                // nothing to do. Leave things as they were.
                return null;
            }
        } else {
            // add or replace old mapping
            put(key, newValue);
            return newValue;
        }
    }

merge

把还没有关联的键设定默认的值

 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);
        V newValue = (oldValue == null) ? value :
                   remappingFunction.apply(oldValue, value);
        if(newValue == null) {
            remove(key);
        } else {
            put(key, newValue);
        }
        return newValue;
    }

猜你喜欢

转载自blog.csdn.net/qq_32726809/article/details/82783581
今日推荐