回味集合(九)Map接口

源码

package java.util;

public interface Map<K,V> {
     // 返回map中的键值对个数
    int size();
    // 判断map中是否包含键值对
    boolean isEmpty();
    // 判断map是否存在指定的key
    boolean containsKey(Object key);
    // 判断map是否存在指定的value
    boolean containsValue(Object value);
    // 在map中通过key获取对应的value
    V get(Object key);
    // 向map存放键值对
    V put(K key, V value);
    // 在map中通过key移除指定的键值对
    V remove(Object key);
    // 向map集合中添加一个map集合
    void putAll(Map<? extends K, ? extends V> m);
    // 从map中删除所有的键值对
    void clear();
    // 从map中获取获取所有的key
    Set<K> keySet();
    //  map集合中的所有键值对象,这些键值对象将存放在另一个集合对象中
    Collection<V> values();
    // 获取map所有的键值对放入Set集合中
    Set<Map.Entry<K, V>> entrySet();
    // 内部接口Entry
    interface Entry<K,V> {
        // 获取key
        K getKey();
       // 获取value
        V getValue();
       // 设置value
        V setValue(V value);
        // 比较
        boolean equals(Object o);
        // hashCode
        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());
        }
    }

    boolean equals(Object o);

    int hashCode();
    // 获取默认
    default V getOrDefault(Object key, V defaultValue) {
        V v;
        // 根据key获取value判断是否为null,如果不为空则返回value否则判断key是否在map中存在
        // 如果存在则返回对应的value否则返回defaultValue传入的默认value
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }
    // 遍历
    default void forEach(BiConsumer<? super K, ? super V> action) {
    // 判断当前对象是否为空
        Objects.requireNonNull(action);
        // 编辑map
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {
            // 获取key
                k = entry.getKey();
            // 获取value    
                v = entry.getValue();
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
            // 将key和value传给BiConsumer接口的中的accept方法
            action.accept(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);
            }
        }
    }
    // 如果集合中不存在value时就放入集合中
    default V putIfAbsent(K key, V value) {
       // 根据key获取对应的value
        V v = get(key);
        // 判断value是否为空,为空时进入方法
        if (v == null) {
            // 将key 和 value 放入map中
            v = put(key, value);
        }
        // 返回value值
        return v;
    }
    // 移除
    default boolean remove(Object key, Object value) {
        // 通过key获取对应的value
        Object curValue = get(key);
        // 判断将要移除的value和通过key获取的value是否相等,相等则不进入方法
        if (!Objects.equals(curValue, value) ||
             // 如果通过key获取的value值为空并且map中不存在key
            (curValue == null && !containsKey(key))) {
            // 返回false 移除失败
            return false;
        }
        // 不相等通过key移除键值对
        remove(key);
        // 返回true 移除成功
        return true;
    }
     // 替换
    default boolean replace(K key, V oldValue, V newValue) {
        // 通过key获取以前的value
        Object curValue = get(key);
        // 判断curValue 和oldValue 是否相等,相等则不进入方法
        if (!Objects.equals(curValue, oldValue) ||
            // 判断curValue为空并且map中不存在key
            (curValue == null && !containsKey(key))) {
            // 返回false 替换失败
            return false;
        }
        // 否则成功
        put(key, newValue);
        return true;
    }
    // 替换
    default V replace(K key, V value) {
        V curValue;
        // 判断通过key获取的value不为空或者map中存在key
        if (((curValue = get(key)) != null) || containsKey(key)) {
            // 替换新的value
            curValue = put(key, value);
        }
        // 返回之前旧的value
        return curValue;
    }

       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;
        }
    }

    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;
        }
    }

    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;
    }
}

简介

  • Map接口中键和值一一映射. 可以通过键来获取值。
  • 给定一个键和一个值,你可以将该值存储在一个Map对象. 之后,你可以通过键来访问对应的值。
  • 当访问的值不存在的时候,方法就会抛出一个NoSuchElementException异常.
  • 当对象的类型和Map里元素类型不兼容的时候,就会抛出一个 ClassCastException异常。
  • 当在不允许使用Null对象的Map中使用Null对象,会抛出一个NullPointerException 异常。
  • 当尝试修改一个只读的Map时,会抛出一个UnsupportedOperationException异常。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值