AbstractMap 提供 Map 接口的骨干实现,以最大限度地减少实现此接口所需的工作。
//实现了接口Map public abstract class AbstractMap<K,V> implements Map<K,V> { protected AbstractMap() {//提供一个空的构造函数 } public int size() {//返回元素个数 return entrySet().size();//通过entrySet()方法获得元素集合 } public boolean isEmpty() { return size() == 0;//判断是否为空 } //判断是否包含值为value的元素,这里的实现是根据迭代器来判断的 public boolean containsValue(Object value) { Iterator<Entry<K,V>> i = entrySet().iterator();//获得元素集合的迭代器 if (value==null) {//value为null while (i.hasNext()) { Entry<K,V> e = i.next(); if (e.getValue()==null) return true; } } else { while (i.hasNext()) { Entry<K,V> e = i.next(); if (value.equals(e.getValue()))//这里比较的是内容 return true; } } return false; } //判断是否包含key为key的元素,这里也是通过元素集合的迭代器来判断 public boolean containsKey(Object key) { Iterator<Map.Entry<K,V>> i = entrySet().iterator();//获得元素集合的迭代器 if (key==null) {//key为null while (i.hasNext()) { Entry<K,V> e = i.next();//迭代器获得Entry类型的元素 if (e.getKey()==null) return true; } } else { while (i.hasNext()) { Entry<K,V> e = i.next(); if (key.equals(e.getKey()))//这里比较的也是内容 return true; } } return false; } //获得key为key的值,这里的实现和上面两个方法类似 public V get(Object key) { Iterator<Entry<K,V>> i = entrySet().iterator(); if (key==null) { while (i.hasNext()) { Entry<K,V> e = i.next(); if (e.getKey()==null) return e.getValue(); } } else { while (i.hasNext()) { Entry<K,V> e = i.next(); if (key.equals(e.getKey())) return e.getValue(); } } return null; } //添加元素方法,这里未实现 public V put(K key, V value) { throw new UnsupportedOperationException(); } //删除key为key的元素 public V remove(Object key) { Iterator<Entry<K,V>> i = entrySet().iterator();//获得迭代器 Entry<K,V> correctEntry = 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; } } V oldValue = null; if (correctEntry !=null) { oldValue = correctEntry.getValue(); i.remove();//通过迭代器的remove方法删除 } return oldValue; } //添加所有元素 public void putAll(Map<? extends K, ? extends V> m) { for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) put(e.getKey(), e.getValue());//循环调用Put方法 } //删除所有元素 public void clear() { entrySet().clear(); } transient volatile Set<K> keySet = null;//key的集合 transient volatile Collection<V> values = null;//values的集合 //返回key的集合 public Set<K> keySet() { if (keySet == null) {//如果为空 keySet = new AbstractSet<K>() {//key的类型为AbstractSet public Iterator<K> iterator() {//实现其迭代器接口 return new Iterator<K>() {//匿名内部类 private Iterator<Entry<K,V>> i = entrySet().iterator();//获得Entry集合的迭代器 public boolean hasNext() {//判断是否有元素 return i.hasNext(); } public K next() {//获得元素的key 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) {//判断是否有键值为k的元素 return AbstractMap.this.containsKey(k);//调用父类的方法来判断 } }; } return keySet; } //返回所有值的集合,实现和上面类似,请参考 public Collection<V> values() { if (values == null) { values = new AbstractCollection<V>() { public Iterator<V> iterator() {//迭代器接口 return new Iterator<V>() {//匿名内部类,迭代器 private Iterator<Entry<K,V>> i = entrySet().iterator();//获得Entry集合的迭代器 public boolean hasNext() { return i.hasNext(); } public V next() { return i.next().getValue(); } 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 v) { return AbstractMap.this.containsValue(v); } }; } return values; } //返回Entry的集合。 public abstract Set<Entry<K,V>> entrySet(); //判断元素是否相等的方法 public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Map)) return false; Map<K,V> m = (Map<K,V>) o; if (m.size() != size()) return false; 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) {//值为null if (!(m.get(key)==null && m.containsKey(key)))//判断m中是否不存在对应的key和alue return false; } else { if (!value.equals(m.get(key))) return false; } } } catch (ClassCastException unused) { return false; } catch (NullPointerException unused) { return false; } return true; } public int hashCode() { int h = 0; Iterator<Entry<K,V>> i = entrySet().iterator(); while (i.hasNext()) h += i.next().hashCode();//每个Entry的hash之和 return h; } public String toString() { Iterator<Entry<K,V>> i = entrySet().iterator(); if (! i.hasNext()) return "{}"; StringBuilder sb = new StringBuilder(); sb.append(‘{‘); for (;;) { Entry<K,V> e = i.next(); K key = e.getKey(); V value = e.getValue(); sb.append(key == this ? "(this Map)" : key); sb.append(‘=‘); sb.append(value == this ? "(this Map)" : value); if (! i.hasNext()) return sb.append(‘}‘).toString(); sb.append(‘,‘).append(‘ ‘); } } //Clone方法 protected Object clone() throws CloneNotSupportedException { AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone(); result.keySet = null;//key为空 result.values = null;//value为空 return result; } //判断对象是否相等 private static boolean eq(Object o1, Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } //Entry接口的实现,是二元组的简单操作 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; } } //只读Entry的实现,只是取消了修改Entry的方法 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; } } }