# 一个效率低下的Map实现：AbstractMap

AbstractMap为Map提供了默认的实现，不过在子类中一般都有覆盖，因为这里的实现效率都比较低,这个类中的实现方法几乎都没有什么价值，仅仅是实现了而已！

public abstract class AbstractMap implements Map

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

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

public boolean containsValue(Object value) {
Iterator i = entrySet().iterator();
if (value==null) {
while (i.hasNext()) {
Entry e = (Entry) i.next();
if (e.getValue()==null)
return true;
}
} else {
while (i.hasNext()) {
Entry e = (Entry) i.next();
if (value.equals(e.getValue()))
return true;
}
}
return false;
}

public boolean containsKey(Object key) {
Iterator i = entrySet().iterator();
if (key==null) {
while (i.hasNext()) {
Entry e = (Entry) i.next();
if (e.getKey()==null)
return true;
}
} else {
while (i.hasNext()) {
Entry e = (Entry) i.next();
if (key.equals(e.getKey()))
return true;
}
}
return false;
}

public Object get(Object key) {
Iterator i = entrySet().iterator();
if (key==null) {
while (i.hasNext()) {
Entry e = (Entry) i.next();
if (e.getKey()==null)
return e.getValue();
}
} else {
while (i.hasNext()) {
Entry e = (Entry) i.next();
if (key.equals(e.getKey()))
return e.getValue();
}
}
return null;
}

public Object put(Object key, Object value) {
throw new UnsupportedOperationException();
}

public Object remove(Object key) {
Iterator i = entrySet().iterator();
Entry correctEntry = null;
if (key==null) {
while (correctEntry==null && i.hasNext()) {
Entry e = (Entry) i.next();
if (e.getKey()==null)
correctEntry = e;
}
} else {
while (correctEntry==null && i.hasNext()) {
Entry e = (Entry) i.next();
if (key.equals(e.getKey()))
correctEntry = e;
}
}

Object oldValue = null;
if (correctEntry !=null) {
oldValue = correctEntry.getValue();
i.remove();
}
return oldValue;
}

public void putAll(Map t) {
Iterator i = t.entrySet().iterator();
while (i.hasNext()) {
Entry e = (Entry) i.next();
put(e.getKey(), e.getValue());
}
}

public void clear() {
entrySet().clear();
}

transient volatile Set keySet = null;
transient volatile Collection values = null;

public Set keySet() {
if (keySet == null) {
keySet = new AbstractSet() {
public Iterator iterator() {
return new Iterator() {
private Iterator i = entrySet().iterator();

public boolean hasNext() {
return i.hasNext();
}
public Object next() {
return ((Entry)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;
}

public Collection values() {
if (values == null) {
values = new AbstractCollection() {
public Iterator iterator() {
return new Iterator() {
private Iterator i = entrySet().iterator();
public boolean hasNext() {
return i.hasNext();
}
public Object next() {
return ((Entry)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;
}

public abstract Set entrySet();

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

if (!(o instanceof Map))
return false;
Map t = (Map) o;
if (t.size() != size())
return false;

try {
Iterator i = entrySet().iterator();
while (i.hasNext()) {
Entry e = (Entry) i.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
if (!(t.get(key)==null && t.containsKey(key)))
return false;
} else {
if (!value.equals(t.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() {
StringBuffer buf = new StringBuffer();
buf.append("{");

Iterator i = entrySet().iterator();
boolean hasNext = i.hasNext();
while (hasNext) {
Entry e = (Entry) (i.next());
Object key = e.getKey();
Object value = e.getValue();
buf.append((key == this ? "(this Map)" : key) + "=" + (value == this ? "(this Map)": value));

hasNext = i.hasNext();
if (hasNext)
buf.append(", ");
}

buf.append("}");
return buf.toString();
}

protected Object clone() throws CloneNotSupportedException {
AbstractMap result = (AbstractMap)super.clone();
result.keySet = null;
result.values = null;
return result;
}

static class SimpleEntry implements Entry {
Object key;
Object value;

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

public SimpleEntry(Map.Entry e) {
this.key = e.getKey();
this.value = e.getValue();
}

public Object getKey() {
return key;
}

public Object getValue() {
return value;
}

public Object setValue(Object value) {
Object 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() {
Object v;
return ((key == null) ? 0 : key.hashCode()) ^((value == null) ? 0 : value.hashCode());
}

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

private static boolean eq(Object o1, Object o2) {
return (o1 == null ? o2 == null : o1.equals(o2));
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：一个效率低下的Map实现：AbstractMap 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)