CSDN博客

img treeroot

一个效率低下的Map实现:AbstractMap

发表于2004/9/20 12:42:00  4195人阅读

分类: Java基础

AbstractMap为Map提供了默认的实现,不过在子类中一般都有覆盖,因为这里的实现效率都比较低,这个类中的实现方法几乎都没有什么价值,仅仅是实现了而已!
声明如下:
public abstract class AbstractMap implements Map
有关Map:http://blog.csdn.net/treeroot/archive/2004/09/20/110331.aspx

这个类的实现都比较简单,几乎都是通过entrySet完成了,所以基本上没有做什么分析,这些代码可以
作为参考:

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;
}
这里返回的是key的Set,这个方法有两个匿名类,可读性比较差,第一个是AbstractSet的子类
然后这个子类的iterator()的方法又生成了一个匿名类:实现Iterator接口,如果对匿名类比较
熟悉的,应该没有什么问题。

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));
  }
}

0 0

相关博文

我的热门文章

img
取 消
img