Map族系 —— AbstractMap

版权声明:有关于交易的部分,如果有使用blog的策略,盈亏自负。https://blog.csdn.net/ydonghao2 https://blog.csdn.net/ydonghao2/article/details/81077783

本文以Java10为背景,Java8到Java10只是增加了一些方法,总体结构没变。 
转载请注明:https://blog.csdn.net/ydonghao2/article/details/81077783
TalBen

这个class提供了Interface Map的一个最基本的实现。可以说在defeult和static方法能在interface 实现之前,为了减少要implement这个interface要花费的功夫,可以实现这个类。其实Java8,9,10之后添加的方法都在Map 中直接添加了,所以这个class未来不排除被替换。但是目前所有要implement Map的方法都extends这个class。

先看看这个设计架构。

所有的implement Map的class都要implement Entry。而这里又有SimpleEntry和SimpleImmutableEntry,首先所有的implement Map的可以修改的都要实现put,否则千万别实现put。

    public static class SimpleEntry<K,V>
        implements Entry<K,V>, java.io.Serializable
    {
        private static final long serialVersionUID = -8499721149061103585L;

        private final K key;
        private V value;

        public SimpleEntry(K key, V value) {
            this.key   = key;
            this.value = value;
        }

        public SimpleEntry(Entry<? extends K, ? extends V> entry) {
            this.key   = entry.getKey();
            this.value = entry.getValue();
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

        public V setValue(V value) {
            V oldValue = this.value;
            this.value = value;
            return oldValue;
        }

        public boolean equals(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
            return eq(key, e.getKey()) && eq(value, e.getValue());
        }

        public int hashCode() {
            return (key   == null ? 0 :   key.hashCode()) ^
                   (value == null ? 0 : value.hashCode());
        }

        public String toString() {
            return key + "=" + value;
        }

    }

    public static class SimpleImmutableEntry<K,V>
        implements Entry<K,V>, java.io.Serializable
    {
        private static final long serialVersionUID = 7138329143949025153L;

        private final K key;
        private final V value;

        public SimpleImmutableEntry(K key, V value) {
            this.key   = key;
            this.value = value;
        }

        public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
            this.key   = entry.getKey();
            this.value = entry.getValue();
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

        public V setValue(V value) {
            throw new UnsupportedOperationException();
        }

        public boolean equals(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
            return eq(key, e.getKey()) && eq(value, e.getValue());
        }

        public int hashCode() {
            return (key   == null ? 0 :   key.hashCode()) ^
                   (value == null ? 0 : value.hashCode());
        }

        public String toString() {
            return key + "=" + value;
        }

    }

SimpleEntry和SimpleImmutableEntry的不同在于setValue方法。二者区别是SimpleEntry是可以随意修改的,但是SimpleImmutableEntry不可以,二者对应的使用场景不一样。二者方法比较简单,就不在这里详解了。

静态类和非静态类重要的区别是在于静态类不能被实例化。SimpleEntry和SimpleImmutableEntry都是静态内部类。


以下来自: https://www.jb51.net/article/74838.htm     

     java允许我们在一个类里面定义静态类。比如内部类(nested class)。把nested class封闭起来的类叫外部类。在java中,我们不能用static修饰顶级类(top level class)。只有内部类可以为static。

     静态内部类和非静态内部类之间到底有什么不同呢?下面是两者间主要的不同。

    (1)内部静态类不需要有指向外部类的引用。但非静态内部类需要持有对外部类的引用。

    (2)非静态内部类能够访问外部类的静态和非静态成员。静态类不能访问外部类的非静态成员。他只能访问外部类的静态成员。

    (3)一个非静态内部类不能脱离外部类实体被创建,一个非静态内部类可以访问外部类的数据和方法,因为他就在外部类里面。


keySet的声明是Set<K>,values是Collection<V>。顾名思义keySet是key的Set容器,可以遍历;values是value的集合。

    public boolean equals(Object o) {
        if (o == this)
            return true;

        if (!(o instanceof Map))
            return false;
        Map<?,?> m = (Map<?,?>) o;
        if (m.size() != size())
            return false;

        try {
            for (Entry<K, V> e : entrySet()) {
                K key = e.getKey();
                V value = e.getValue();
                if (value == null) {
                    if (!(m.get(key) == null && m.containsKey(key)))
                        return false;
                } else {
                    if (!value.equals(m.get(key)))
                        return false;
                }
            }
        } catch (ClassCastException unused) {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }

        return true;
    }

AbstractMap提供的equals是比较key-value值而得,和顺序无关。

其它的方法不细讲,方法类似。

猜你喜欢

转载自blog.csdn.net/ydonghao2/article/details/81077783