AbstractMap

原文链接

AbstractMap对Map接口做了通用的实现,其他Map的实现类可以继承AbstractMap来减少重复编码。

查询操作

返回map的大小:

public int size() {
    return entrySet().size();
}

判断是否为空:

public boolean isEmpty() {
    return size() == 0;
}

判断是否包含指定的值:

//迭代entrySet来查找
public boolean containsValue(Object value) {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    //查找value为null的值
    if (value==null) {
        while (i.hasNext()) {
            Entry<K,V> e = i.next();
            if (e.getValue()==null)
                return true;
        }
    } else {
    //value不为null的时候,使用equals方法判断
        while (i.hasNext()) {
            Entry<K,V> e = i.next();
            if (value.equals(e.getValue()))
                return true;
        }
    }
    return false;
}

查找是否包含指定的key:

//实现跟containsValue类似
public boolean containsKey(Object key) {}

根据指定的key获取value:

//实现跟containsValue类似,只不过在这里返回的是匹配的value
public V get(Object key){}

修改操作

//put方法不支持,不做实现
public V put(K key, V value) {
    throw new UnsupportedOperationException();
}

根据指定的key删除对应的entrySet:

public V remove(Object key) {
    //获得迭代器
    Iterator<Entry<K,V>> i = entrySet().iterator();
    //存储根据key找到的entry
    Entry<K,V> correctEntry = null;
    //key为null
    if (key==null) {
        //如果已经找到过一次,就不再继续找了
        while (correctEntry==null && i.hasNext()) {
            Entry<K,V> e = i.next();
            if (e.getKey()==null)
                correctEntry = e;
        }
    } else {
        while (correctEntry==null && i.hasNext()) {
            Entry<K,V> e = i.next();
            if (key.equals(e.getKey()))
                correctEntry = e;
        }
    }
    //将当前的entry删除,返回原来的值
    V oldValue = null;
    if (correctEntry !=null) {
        oldValue = correctEntry.getValue();
        i.remove();
    }
    return oldValue;
}

批量操作

把指定的Map中所有映射复制到当前的Map中去:

public void putAll(Map<? extends K, ? extends V> m) {
    //遍历指定的Map,挨个put
    for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
        put(e.getKey(), e.getValue());
}

清空Map:

public void clear() {
    entrySet().clear();
}
//key集合
transient volatile Set<K>        keySet = null;
//value集合
transient volatile Collection<V> values = null;

返回key的集合,set集合只在第一次调用该方法的时候创建。不是线程安全的,不保证多次请求的结果都是一样的。

public Set<K> keySet() {
    //只在keySet为空的时候才做处理
    if (keySet == null) {
        keySet = new AbstractSet<K>() {
            public Iterator<K> iterator() {
                return new Iterator<K>() {
                    private Iterator<Entry<K,V>> i = entrySet().iterator();

                    public boolean hasNext() {
                        return i.hasNext();
                    }

                    public K next() {
                        return i.next().getKey();
                    }

                    public void remove() {
                        i.remove();
                    }
                };
            }

            public int size() {
                return AbstractMap.this.size();
            }

            public boolean isEmpty() {
                return AbstractMap.this.isEmpty();
            }

            public void clear() {
                AbstractMap.this.clear();
            }

            public boolean contains(Object k) {
                return AbstractMap.this.containsKey(k);
            }
        };
    }
    return keySet;
}

返回values集合,实现跟keySet类似:

public Collection<V> values() {}

比较和哈希

public boolean equals(Object o) {
    //给定的对象是当前对象,返回true
    if (o == this)
        return true;
    //非map类型的,返回false
    if (!(o instanceof Map))
        return false;
    Map<K,V> m = (Map<K,V>) o;
    //大小不一样,返回false
    if (m.size() != size())
        return false;

    //迭代判断每个entrySet
    try {
        Iterator<Entry<K,V>> i = entrySet().iterator();
        while (i.hasNext()) {
            Entry<K,V> e = i.next();
            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;
}
//哈希值为每个entrySet的哈希值相加
public int hashCode() {
    int h = 0;
    Iterator<Entry<K,V>> i = entrySet().iterator();
    while (i.hasNext())
        h += i.next().hashCode();
    return h;
}
public String toString() {}

克隆:

//只做浅拷贝,key和value集合不做克隆
protected Object clone() throws CloneNotSupportedException {
    AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
    result.keySet = null;
    result.values = null;
    return result;
}

从1.6开始新增的

public static class SimpleEntry<K,V>

public static class SimpleImmutableEntry<K,V>

上面两个Simple是对Map中interface Entry<K,V>的实现,SimpleImmutableEntry不允许setValue。

转载于:https://my.oschina.net/dachengxi/blog/864431

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值