AbstractMap:
AbstractMap
AbstractMap继承了Map,但没有实现entrySet()方法(该方法还是abstract修饰),如果要继承AbstractMap,需要自己实现entrySet()方法。没有真正实现put(K key, V value)方法,这里“没有真正实现”的意思是,该方法在形式上已经实现了,即没有用abstract修饰了,但是方法内部仅仅是抛出个异常,并没有真正实现方法体内容,从下面的源码中可以看到。
此类提供 Map 接口的骨干实现,以最大限度地减少实现此接口所需的工作。
要实现不可修改的映射,编程人员只需扩展此类并提供 entrySet 方法的实现即可,该方法将返回映射的映射关系 set 视图。通常,返回的 set 将依次在AbstractSet 上实现。此 set 不支持add 或 remove 方法,其迭代器也不支持 remove 方法。
要实现可修改的映射,编程人员必须另外重写此类的 put 方法(否则将抛出 UnsupportedOperationException),entrySet().iterator() 返回的迭代器也必须另外实现其remove 方法。
按照 Map 接口规范中的建议,编程人员通常应该提供一个 void(无参数)构造方法和 map 构造方法。
此类中每个非抽象方法的文档详细描述了其实现。如果要实现的映射允许更有效的实现,则可以重写所有这些方法
public abstract class AbstractMap<K,V> implements Map<K,V>
AbstractMap源码:
-
//抽象类
-
public abstract class AbstractMap<K,V> implements Map<K,V> {
-
/**
-
* 空构造函数
-
*/
-
protected AbstractMap() {
-
}
-
/*----------查询操作--------------------*/
-
public int size() {
-
return entrySet().size();
-
}
-
public boolean isEmpty() {
-
return size() == 0;
-
}
-
// 如果此映射将一个或多个键映射到指定值,则返回 true。
-
public boolean containsValue(Object value) {
-
Iterator<Entry<K,V>> i = entrySet().iterator();//entrySet要自己实现
-
if (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;
-
}
-
//如果此映射包含指定键的映射关系,则返回 true。
-
public boolean containsKey(Object key) {
-
Iterator<Map.Entry<K,V>> i = entrySet().iterator();
-
if (key==null) {
-
while (i.hasNext()) {
-
Entry<K,V> e = i.next();
-
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;
-
}
-
//返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
-
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) //AbstractMap 的键值对可以为NULL
-
return e.getValue();
-
}
-
} else {
-
while (i.hasNext()) {
-
Entry<K,V> e = i.next();
-
if (key.equals(e.getKey()))
-
return e.getValue();
-
}
-
}
-
return null;
-
}
-
/*----------修改操作--------------------*/
-
//将指定的值与此映射中的指定键关联
-
//要实现可修改的映射,必须另外重写此类的 put 方法
-
public V put(K key, V value) {
-
throw new UnsupportedOperationException();
-
}
-
//如果存在一个键的映射关系,则将其从此映射中移除
-
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();
-
}
-
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());
-
}
-
public void clear() {
-
entrySet().clear();
-
}
-
/*----------视图--------------------*/
-
transient volatile Set<K> keySet = null;
-
transient volatile Collection<V> values = null;
-
public Set<K> 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;
-
}
-
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();
-
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;
-
}
-
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) {
-
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;
-
}
-
//Map<K,V>的hash值为每个映射的hash总和
-
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() {
-
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(' ');
-
}
-
}
-
//返回此 AbstractMap 实例的浅表副本:不复制键和值本身。
-
protected Object clone() throws CloneNotSupportedException {
-
AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
-
result.keySet = null;
-
result.values = null;
-
return result;
-
}
-
private static boolean eq(Object o1, Object o2) {
-
return o1 == null ? o2 == null : o1.equals(o2);
-
}
-
/*SimpleEntry实现了Map类中的Entry接口,
-
另外也实现了Serializable接口,可序列化
-
*/
-
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;
-
}
-
}
-
/*SimpleEntry实现了Map类中的Entry接口,
-
另外也实现了Serializable接口,可序列化
-
*/
-
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());
-
}
-
//重写了toString方法,返回key=value形式
-
public String toString() {
-
return key + "=" + value;
-
}
-
}
-
}
从源码中可以看出,AbstractMap类提供了Map接口的主要实现,以最大限度地减少了实现此接口所需的工作,但是AbstractMap没有实现entrySet()方法。所以如果我们要实现不可修改的Map时,只需要扩展此类并提供entrySet()方法的实现即可,另外从源码中也可以看出,entrySet()方法返回的Set(即keySet)不支持add()或remove()方法。如果我们要实现可修改的Map,那么就必须另外重写put()方法,否则将抛出UnsupportedOperationException,而且entrySet.iterator()返回的迭代器i也必须实现其remove方法。
不过一般我们不需要自己实现Map,因为已经有Map的实现类了,如HashMap和TreeMap等。
AbstractMap 结构:
其中:SimpleImmutableEntry<K,V>和SimpleEntry<K,V>两个嵌套类会在AbstractMap的子类中用到
-
public static class SimpleEntry<K,V>
-
implements Entry<K,V>, java.io.Serializable
-
public static class SimpleImmutableEntry<K,V>
-
implements Entry<K,V>, java.io.Serializable