java_集合体系之Map框架相关抽象类接口详解、源码――08(二)

2014-11-24 07:32:21 · 作者: · 浏览: 1
iterator(); if (key==null) { while (i.hasNext()) { Entry e = i.next(); if (e.getKey()==null) return e.getValue(); } } else { while (i.hasNext()) { Entry e = i.next(); if (key.equals(e.getKey())) return e.getValue(); } } return null; } // Modification Operations /** 将一个映射放入到Map中、要求子类要有自己的实现*/ public V put(K key, V value) { throw new UnsupportedOperationException(); } /** 删除键为key的映射*/ public V remove(Object key) { Iterator > i = entrySet().iterator(); Entry correctEntry = null; if (key==null) { while (correctEntry==null && i.hasNext()) { Entry e = i.next(); if (e.getKey()==null) correctEntry = e; } } else { while (correctEntry==null && i.hasNext()) { Entry e = i.next(); if (key.equals(e.getKey())) correctEntry = e; } } V oldValue = null; if (correctEntry !=null) { oldValue = correctEntry.getValue(); i.remove(); } return oldValue; } // Bulk Operations /** 将指定Map中所有键值对都放入到当前Map中*/ public void putAll(Map m) { for (Map.Entry e : m.entrySet()) put(e.getKey(), e.getValue()); } /** 清空当前Map*/ public void clear() { entrySet().clear(); } // Views transient volatile Set keySet = null; transient volatile Collection values = null; /** 获取有当前Map中所有key组成的Set*/ public Set keySet() { if (keySet == null) { //通过匿名类的方式获取keySet实体类 keySet = new AbstractSet () { //实现获取Iterator的方法、并实现Iterator内部方法 public Iterator iterator() { return new Iterator () { private Iterator > 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 contains(Object k) { return AbstractMap.this.containsKey(k); } }; } return keySet; } /** 获取当前Map所有value组成的集合*/ public Collection values() { if (values == null) { //通过匿名类的方式获取Collection实体类 values = new AbstractCollection
() { //实现获取Iterator的方法、并实现Iterator内部方法 public Iterator iterator() { return new Iterator () { private Iterator > i = entrySet().iterator(); 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 contains(Object v) { return AbstractMap.this.containsValue(v); } }; } return values; } /** 获取由当前Map中所有映射组成的Set*/ public abstract Set > entrySet(); // Comparison and hashing public boolean equals(Object o) { //如果是他本身、返回true if (o == this) return true; //如果传入的不是Map、返回false if (!(o instanceof Map)) return false; Map m = (Map ) o; //优化操作、如果传入的Map的size与当前被比较的Map的size不同、那么就可确定这两个Map不相等 if (m.size() != size()) return false; //比较两个Map中的所有value是否相等、相等则返回true、不相等则返回false try { Iterator > i = entrySet().iterator(); while (i.hasNext()) { Entry 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; } /** 返回此映射的哈希码值。*/ public int hashCode() { int h = 0; Iterator > i = entrySet().iterator(); while (i.hasNext()) h += i.next().hashCode(); return h; } /** 返回此映射的字符串表示形式。*/ public String toString() { Iterator > i = entrySet().iterator(); if (! i.hasNext()) return "{}"; StringBuilder sb = new StringBuilder(); sb.append('{'); for (;;) { Entry e = i.next(); K key = e.getKey(); V value = e.getValue(); s