Map(1.8)接口

public interface Map<K,V> {
    // Query Operations

    /**
     * 返回此映射中的键值映射数。
     * 如果映射包含多个<tt> Integer.MAX_VALUE </ tt>元素,则返回
     * <tt> Integer.MAX_VALUE </ tt>。
     *
     * @return 此映射中的键值映射数
     */
    int size();

    /**
     * Returns <tt> true </ tt>(如果此映射不包含键值映射)。
     *
     * @return <tt> true </ tt>(如果此映射不包含键值映射)。
     */
    boolean isEmpty();

    /**
     * Returns <tt> true </ tt>(如果此映射不包含键值映射)键
     * 更正式地讲,当且仅当此映射包含键<tt> k </ tt>的映射时,才返回<tt> 
     * true </tt>,从而使<tt>(key == null?k == null:key。等于(k))</tt> .
     * (最多可以有一个这样的映射。
     * @param key是否存在于此map中的密钥
     * @return <tt>true</tt> 如果map中包含指定的key
     * @throws ClassCastException  如果这个map的key类型与这个key不符合
     * @throws NullPointerException 如果指定的键为null并且此映射不允许使用空键
     */
    boolean containsKey(Object key);

    /**
     * 如果此映射将一个或多个键映射到该键,则返回<tt> true </ tt>指定值。
     * 更正式地,当且仅当<tt> true </ tt>此映射至少包含一个与值<tt> v </ tt>的映射, 
     * 这样
     * <tt>(value == null?v == null:value.equals(v))</ tt>。此操作大多数情况
     * 下,map尺寸可能需要时间线性
     * <tt> Map </ tt>接口的实现。
     * @param  key是否存在于此map中以进行测试
    
     */
    boolean containsValue(Object value);

    /**
     *返回指定键映射到的值,
     *或{@code null}(如果此映射不包含键的映射)。
     * <p>更正式的说法是,如果此映射包含键的映射
     * {@code k}为值{@code v},使得{@code(key == null?k == null:
     * key.equals(k))},则此方法返回{@code v};除此以外
     * 它返回{@code null}。 (最多可以有一个这样的映射。)
     * @param 键返回其关联值的键
     * @throws ClassCastException NullPointerException
     */
    V get(Object key);

    // 添加操作

    /**
     * 将指定值与此映射中的指定键相关联
     * (可选操作)。如果map先前包含的映射键,旧值将替换为指定值。 
     */
    V put(K key, V value);

    /**
     * 从此映射中删除键的映射(如果存在)
     *  <p>返回此映射先前与该键相关联的值,
     *  或<tt> null </ tt>(如果该映射不包含该键的映射)。
     * @param 要从map中删除其映射的键
     * @throws UnsupportedOperationException ClassCastException    
     *  NullPointerException 
     */
    V remove(Object key);


    // 批量操作

    /**
     * 将所有映射从指定映射复制到此映射
     * (可选操作)。此调用的效果等同于 一次在此地图上调用{@link #put(Object,
     * Object)put(k,v)}
     * 对于键<tt> k </ tt>中每个从键<tt> k </ tt>到值<tt> v </ tt>的映射
     * 指定地图。如果以下操作未定义此操作的行为
     * 指定的映射在操作过程中被修改。
     *
     * @param 需要存储的map
     * @throws UnsupportedOperationException ClassCastException
     * NullPointerException IllegalArgumentException
     */
    void putAll(Map<? extends K, ? extends V> m);

    /**
    * 从此映射中删除所有映射(可选操作)。
     *此调用返回后,map将为空。
     *
     * @throws UnsupportedOperationException 如果map不支持mapclear操作
     */
    void clear();


    // 观看次数
    Set<K> keySet();

    Collection<V> values();

    Set<Map.Entry<K, V>> entrySet();

    /**
     * @see Map#entrySet()
     * @since 1.2
     */
    interface Entry<K,V> {
        
        K getKey();

 
        V getValue();

        /**
         * @param value new value to be stored in this entry
         * @return old value corresponding to the entry
         */
        V setValue(V value);

        /**
         * @param o object to be compared for equality with this map entry
         * @return <tt>true</tt> if the specified object is equal to this map
         *         entry
         */
        boolean equals(Object o);

        /**
         * @return the hash code value for this map entry
         * @see Object#hashCode()
         * @see Object#equals(Object)
         * @see #equals(Object)
         */
        int hashCode();

        /**
         * @param  <K> the {@link Comparable} type of then map keys
         * @param  <V> the type of the map values
         * @return a comparator that compares {@link Map.Entry} in natural order on key.
         * @see Comparable
         * @since 1.8
         */
        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());
        }

        /**
         * @param <K> the type of the map keys
         * @param <V> the {@link Comparable} type of the map values
         * @return a comparator that compares {@link Map.Entry} in natural order on value.
         * @see Comparable
         * @since 1.8
         */
        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());
        }

        /**
         * @param  <K> the type of the map keys
         * @param  <V> the type of the map values
         * @param  cmp the key {@link Comparator}
         * @return a comparator that compares {@link Map.Entry} by the key.
         * @since 1.8
         */
        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());
        }

        /**
         * @param  <K> the type of the map keys
         * @param  <V> the type of the map values
         * @param  cmp the value {@link Comparator}
         * @return a comparator that compares {@link Map.Entry} by the value.
         * @since 1.8
         */
        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());
        }
    }

    // 比较与hashing

    /**
     * @param o object to be compared for equality with this map
     * @return <tt>true</tt> if the specified object is equal to this map
     */
    boolean equals(Object o);

    /**
     * @return the hash code value for this map
     * @see Map.Entry#hashCode()
     * @see Object#equals(Object)
     * @see #equals(Object)
     */
    int hashCode();

    // Defaultable methods

    /**
     * @since 1.8
     */
    default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }

    /**
     * @since 1.8
     */
    default void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        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);
        }
    }

    /**
     * @since 1.8
     */
    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);
            }
        }
    }

    /**
     * @since 1.8
     */
    default V putIfAbsent(K key, V value) {
        V v = get(key);
        if (v == null) {
            v = put(key, value);
        }

        return v;
    }

    /**
     * @since 1.8
     */
    default boolean remove(Object key, Object value) {
        Object curValue = get(key);
        if (!Objects.equals(curValue, value) ||
            (curValue == null && !containsKey(key))) {
            return false;
        }
        remove(key);
        return true;
    }

    /**
     * @since 1.8
     */
    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;
    }

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

    /**
     * @since 1.8
     */
    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;
    }

    /**
     * @since 1.8
     */
    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;
        }
    }

    /**
     * @since 1.8
     */
    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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值