Map源码

package java.util;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.io.Serializable;

// Map是一个接口
public interface Map<K,V> {
	
	// Map中键值对的数量
    int size();
    // 判断此map是否为空
    boolean isEmpty();
    // 判断传入key是否在map中存在
    boolean containsKey(Object key);
    // 判断传入的value是否在map中存在
    boolean containsValue(Object value);
    // 通过key获取到值
    V get(Object key);
    // 向map中插入数据
    V put(K key, V value);
    // 根据key移除键值对
    V remove(Object key);
    // 向当前的map中存入别一个map
    void putAll(Map<? extends K, ? extends V> m);
    // map清空
    void clear();
    // 返回此map中的所有的key的set集合
    Set<K> keySet();
    // 获取此map中的values集合
    Collection<V> values();
    // 返回此map中包含的映射的Set视图。
    Set<Map.Entry<K, V>> entrySet();
    
    // 内部接口
    // 它表示Map中的一个实体(一个key-value对)
    interface Entry<K,V> {
    	// 获取到此条数据的key
        K getKey();
        // 获取到此条数据的value
        V getValue();
        // 设置此条数据的value的值
        V setValue(V value);
        // 比较
        boolean equals(Object o);
        // 计算hashCode()的值
        int hashCode();
        
        // 经过测试下面的几个方法均不能使用
        // 返回一个比较器,比较Map.Entry按键使用给定的Comparator。
        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());
        }
        // 返回一个比较器,比较Map.Entry按键使用给定的Comparator
        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());
        }
        // 返回一个比较器,比较Map.Entry的自然顺序值。 
        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());
        }
        // 返回一个比较器 ,使用给定的Comparator比较Map.Entry的值。
        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());
        }
    }
    
    // 将指定的对象与此映射进行比较以获得相等性
    boolean equals(Object o);
    
    // 计算此map的hashcode
    int hashCode();

    // default即不加任何访问修饰符,通常称为“默认访问权限“或者“包访问权限”。该模式下,只允许在同一个包中进行访问。
    // 通过key获取到value值,如果key或者value为空那么返回这里传入的defaultVale值
    default V getOrDefault(Object key, V defaultValue) {
        V v;
        // 判断传入的key对应的值不为空,或者包含
        return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue;
    }

    // 对此映射中的每一项执行给定的操作,直到所有条目都被处理或操作引发异常。
    default void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        // 对当前的数据进行遍历
        for (Map.Entry<K, V> entry : entrySet()) {
        	// 定义key值
            K k;
            // 定义value值
            V v;
            try {
            	// 分别获取的key与value值
                k = entry.getKey();
                v = entry.getValue();
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
            // 将key-value不断的传给action
            action.accept(k, v);
        }
    }

    // 替换此map中的所有的value,value通过BiFunction中的apply进行处理
    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) {
                throw new ConcurrentModificationException(ise);
            }
            // 获取到处理之后的value值
            v = function.apply(k, v);

            try {
            	// 将处理之后的值重新设置给value
                entry.setValue(v);
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
        }
    }

    // 如果指定的键尚未与值相关联(或映射到 null )将其与给定值相关联并返回 null ,否则返回当前值。 
    default V putIfAbsent(K key, V value) {
        V v = get(key);
        if (v == null) {
            v = put(key, value);
        }
        return v;
    }

    // 根据传入的key-value移除对应的key-value,传入的key-value必须在map中的对应相同的key-value
    default boolean remove(Object key, Object value) {
    	// 获取到对应key的value值
        Object curValue = get(key);
        // 判断传入的key-value在当前map中是否有对应的key-value
        if (!Objects.equals(curValue, value) || (curValue == null && !containsKey(key))) {
            return false;
        }
        remove(key);
        return true;
    }

    // 根据传入的key-value值,将oldValue替换为newValue, 条件key存在且传入的oldvalue与get(key)对应的值相等
    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;
    }

    // 根据传入的key-value,替换对应的key-value,此map中必须包含key;value是否对应有值不影响
    default V replace(K key, V value) {
        V curValue;
        // (curValue = get(key)) != null, 赋值之后再判断
        if (((curValue = get(key)) != null) || containsKey(key)) {
            curValue = put(key, value);
        }
        // 这里返回false,被替换的value
        return curValue;
    }

    // 如果指定的键尚未与值相关联(或映射到null ),则尝试使用给定的映射函数计算其值,并将其输入到此映射中,除非null 。 
    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;
    }

    // 如果指定的密钥的值存在且非空,则尝试计算给定密钥及其当前映射值的新映射。
    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;
        }
    }

    // 尝试计算指定密钥及其当前映射值的映射(如果没有当前映射,则null )。 例如,要为值映射创建或附加一个String msg: 
    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;
        }
    }

    // 如果指定的键尚未与值相关联或与null相关联,则将其与给定的非空值相关联
    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;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值