JDK8源码-Map接口

最近想把查找树、B树、红黑树都用java实现一遍,在JDK中TreeMap类是红黑树的具体实现,其定义如下:

public class TreeMap<K,V>  extends AbstractMap<K,V>
    implements NavigableMap<K,V>, Cloneable, java.io.Serializable{...}

jdk提供的抽象类AbstractMap<K,V>本身就是作为key-value存储的抽象容器,可以在自己实现查找树时用一下,先看定义:

public abstract class AbstractMap<K,V> implements Map<K,V> {...}

AbstractMap<K,V>实现了Map<K,V>接口,该接口内定义了内部接口Entry<K,V>,规范了查询操作、修改操作、视图、批量操作、默认工具方法、:

内部接口Entry<K,V>:也就是在map存储的键值对具体实现类所需要实现的接口,其中规定了键值对的基本操作、hashcode的计算。因为键值对涉及到比较操作,在jdk8中,使用lambda表达式提供了返回比较器的几个方法。

    /**
     * map中存储的键值对所需要实现的接口
     */
    interface Entry<K,V> {
        K getKey();
        V getValue();
        V setValue(V value); //替换

        /**
         * (e1.getKey()==null ? e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &amp;&amp;
         * (e1.getValue()==null ?  e2.getValue()==null : e1.getValue().equals(e2.getValue()))
         */
        boolean equals(Object o);

        /**
         * 返回该entry的hashcode,map中entry的hashcode定义为:
         *     
         *  (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
         *  (e.getValue()==null ? 0 : e.getValue().hashCode())
         * 
         * 保证对于任意两个entry,当e1.equals(e2)时,e1.hashCode()==e2.hashCode()
         */
        int hashCode();

        /**
         * 返回一个对entry的key自然排序的比较器,该段代码使用了jdk8引入的Lambda表达式,等同于
         *       return (Comparator<Map.Entry<K, V>> & Serializable)
            		new Comparator<Map.Entry<K,V>>() {

						@Override
						public int compare(Entry<K, V> c1, Entry<K, V> c2) {
							return c1.getKey().compareTo(c2.getKey());
						}
            				
					};
         */
        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());
        }

        /**
         * 返回一个对entry的value自然排序的比较器,其他同上
         */
        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());
        }

        /**
         * 返回一个对entry的key使用传入的比较器进行比较排序的比较器
         */
        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());
        }

        /**
         *返回一个对entry的value使用传入的比较器进行比较排序的比较器
         */
        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());
        }
    }

查询操作:

/** **************** 查询操作********************* */
	
int size();//返回map容器中key-value对的格式,如果其数量超过Integer.MAX_VALUE,就返回Integer.MAX_VALUE
boolean isEmpty();//返回map是不是空的
   
boolean containsKey(Object key); //map包含键为key的键值对时,返回true,否则返回false,map中的键不可重复
boolean containsValue(Object value);// map中至少包含一个值为value的键值对时,返回true,否则返回false
    
V get(Object key);//查找map中以key为键的键值对,若存在,返回其值,否则返回null

修改操作 :

/** *************** 修改操作 ********************* */ 

    /**
     * 若map当前已经包含以key为键的键值对,则将旧value替换为传入的value,并将旧value返回
     * 若不存在,则将传入的键值对添加到map中,并返回null
     */
    V put(K key, V value);

    /**
     * 查找以key为键的键值对,若存在,移除该键值对,并返回该键值对的value(可能为null),若不存在,则返回null
     */
    V remove(Object key);


    /** **************** 块操作(批量操作)**************** */

    /**
     * 复制m中的所有键值对到当前的map对象中,该操作存在线程安全问题,若在此方法执行时,
     * m被其他线程修改,则结果具有不确定性
     */
    void putAll(Map<? extends K, ? extends V> m);
    void clear(); // 移除所有键值对

视图:对于视图,个人理解有些像mysql中视图的作用,主要还是用于查询操作,但对视图的修改会造成源map内容的变化

    /**
     * 返回当前map中键的集合,keyset由map中的信息支撑,对keyset的更改,会导致map本身的修改,
     * 对map的修改,同样会导致keyset内容的变化。
     * 如果通过keyset获得迭代器iteration,在迭代过程中若成功对keyset或map进行修改,则结果不确定,
     * 会引发java.util.ConcurrentModificationException异常
     * keyset不支持add(),addAll()操作
     */
    Set<K> keySet();

    /**
     * 返回当前map中值的集合,对该集合进行各种操作的影响同keyset
     */
    Collection<V> values();

    /**
     *返回当前map中的键值对集合,各种修改操作影响同上
     */
    Set<Map.Entry<K, V>> entrySet();

 批量操作:通过一张map添加和清除操作

    /**
     * 复制m中的所有键值对到当前的map对象中,该操作存在线程安全问题,若在此方法执行时,
     * m被其他线程修改,则结果具有不确定性
     */
    void putAll(Map<? extends K, ? extends V> m);
    void clear(); // 移除所有键值对

 比较和散列:主要还是定义了hashcode的计算方法

     boolean equals(Object o);

    /**
     * 返回当前map的hashcode,map的hashcode定义为entrySet()中所有entry对应的hashcode的和. 
     */
    int hashCode();

大量的默认方法作为操作工具:用了很多jdk函数编程的新特性

 /** ************************* 默认方法 *********************************** */

    /**
     * get(Object key) 方法返回值为null时有两种情况:
     * 一种是map中没有以key为键的entry,另一种是以key为键的entry的value为null,
     * 下面这个默认方法提供了解决返回值为null时区分两种结果的方法,在调用取值方法时,传入一个默认值defaultValue,
     * 若key存在,则返回key对应的value,否则返回defaultValue
     */
    default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }

    /**
     * forEach()是对外提供的Lambda编程接口,用于定义消费者行为,可以在外部这样使用:
     * map.forEach((k,v)->{
     * 	System.out.println("key:" + k + " value:" + v);
     * });
     * @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);
        }
    }

    /**
     * 用给定的方式替换所有entry的value,类似上一个默认方法
     * @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;
    }

    /**
     * 移除
     */
    default boolean remove(Object key, Object value) {
        Object curValue = get(key); // 得到key对应的value,此时返回值有可能是null
        if (!Objects.equals(curValue, value) ||   // 如果key对应的值与传入的value不相等
            (curValue == null && !containsKey(key))) { // 不存在该key并且get()方法取得的值为null
            return false;
        }
        remove(key);
        return true;
    }

    /**
     * 若当前存在key-oldValue,则替换为key-newValue
     * @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;
    }

    /**
     * 若存在key,则将其value属性置为value.
     * @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;
    }

    /**
     * 当key存在,若其value属性为null,使用传入的方法计算得到的新值newValue,若newValue不为null,
     * 则将key对应的value属性置为该值,并返回该值
     * 若value属性不为null、key不存在或newValue为null,则返回null
     * @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;
    }

    /**
     * 类似上面的方法,但是计算的新值若为null,则会将该键删除
     * @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;
        }
    }

    /**
     * 例如,要将msg创建或附加到值映射:  
     * map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))}   
     * @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)) {
                remove(key); // something to remove
                return null;
            } else {
                return null;// nothing to do. Leave things as they were.
            }
        } else {
            put(key, newValue);// 添加或替换旧值
            return newValue;
        }
    }

    /**
     * 举例, 为一个key对应的value创建或append值为msg的字符串:
     * map.merge(key, msg, String::concat)
     * @since 1.8
     */
    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;
    }

jdk8的Map接口中用了很多新引入的函数式编程的特性。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值