AbstractMap对Map接口做了通用的实现,其他Map的实现类可以继承AbstractMap来减少重复编码。
查询操作
返回map的大小:
public int size() {
return entrySet().size();
}
判断是否为空:
public boolean isEmpty() {
return size() == 0;
}
判断是否包含指定的值:
//迭代entrySet来查找
public boolean containsValue(Object value) {
Iterator<Entry<K,V>> i = entrySet().iterator();
//查找value为null的值
if (value==null) {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (e.getValue()==null)
return true;
}
} else {
//value不为null的时候,使用equals方法判断
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (value.equals(e.getValue()))
return true;
}
}
return false;
}
查找是否包含指定的key:
//实现跟containsValue类似
public boolean containsKey(Object key) {}
根据指定的key获取value:
//实现跟containsValue类似,只不过在这里返回的是匹配的value
public V get(Object key){}
修改操作
//put方法不支持,不做实现
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
根据指定的key删除对应的entrySet:
public V remove(Object key) {
//获得迭代器
Iterator<Entry<K,V>> i = entrySet().iterator();
//存储根据key找到的entry
Entry<K,V> correctEntry = null;
//key为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;
}
}
//将当前的entry删除,返回原来的值
V oldValue = null;
if (correctEntry !=null) {
oldValue = correctEntry.getValue();
i.remove();
}
return oldValue;
}
批量操作
把指定的Map中所有映射复制到当前的Map中去:
public void putAll(Map<? extends K, ? extends V> m) {
//遍历指定的Map,挨个put
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
put(e.getKey(), e.getValue());
}
清空Map:
public void clear() {
entrySet().clear();
}
//key集合
transient volatile Set<K> keySet = null;
//value集合
transient volatile Collection<V> values = null;
返回key的集合,set集合只在第一次调用该方法的时候创建。不是线程安全的,不保证多次请求的结果都是一样的。
public Set<K> keySet() {
//只在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;
}
返回values集合,实现跟keySet类似:
public Collection<V> values() {}
比较和哈希
public boolean equals(Object o) {
//给定的对象是当前对象,返回true
if (o == this)
return true;
//非map类型的,返回false
if (!(o instanceof Map))
return false;
Map<K,V> m = (Map<K,V>) o;
//大小不一样,返回false
if (m.size() != size())
return false;
//迭代判断每个entrySet
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;
}
//哈希值为每个entrySet的哈希值相加
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() {}
克隆:
//只做浅拷贝,key和value集合不做克隆
protected Object clone() throws CloneNotSupportedException {
AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
result.keySet = null;
result.values = null;
return result;
}
从1.6开始新增的
public static class SimpleEntry<K,V>
public static class SimpleImmutableEntry<K,V>
上面两个Simple是对Map中interface Entry<K,V>
的实现,SimpleImmutableEntry不允许setValue。