AbstractMap抽象类
第1部分 AbstractMap介绍
AbstractMap简介
AbstractMap继承自Map接口,具有Map的所有方法,即针对K, V, Entry三种数据类型的方法以及一些集合的方法。
AbstractMap中实现了除entrySet外的所有方法,并且提供了SimpleEntry和SimpleImmutableEntry两种内部类。
AbstractMap构造函数
修饰语和返回类型 | 方法 | 描述 |
---|---|---|
protected | AbstractMap() |
AbstractMap常用API
修饰语和返回类型 | 方法 | 描述 |
---|---|---|
int | size() | 获取容量 |
boolean | isEmpty() | 无元素 |
boolean | containsKey(Object key) | 是否包含key |
V | get(Object key) | 获取key对应的值 |
V | remove(Object key) | 移除key对应的元素 |
void | putAll(Map<? extends K, ? extends V> m) | 将m中的元素都添加进来 |
void | clear() | 清空元素 |
Set | keySet() | 获取key集合 |
Collection | values() | 获取value集合 |
boolean | equals(Object o) | 判断map是否与o等价 |
String | toString() | 转化为字符串 |
第2部分 AbstractMap数据结构
AbstractMap的继承关系
java.lang.Object
↳
public abstract class AbstractMap<K,V> implements Map<K,V> {}
AbstractMap的关系图
图1 AbstractMap的关系图
第3部分 AbstractMap源码解析(基于JDK-8u201)
public abstract class AbstractMap<K,V> implements Map<K,V> {
protected AbstractMap() {
}
public int size() {
return entrySet().size();
}
public boolean isEmpty() {
return size() == 0;
}
//包含value
public boolean containsValue(Object value) {
//获取Set集合的迭代器
Iterator<Entry<K,V>> i = entrySet().iterator();
//分null和非null迭代
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;
}
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;
}
//获取key对应的值
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)
return e.getValue();
}
} else {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (key.equals(e.getKey()))
return e.getValue();
}
}
return null;
}
//此方法标记为不支持,需要子类来实现
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
//移除key对应的元素
public V remove(Object key) {
Iterator<Entry<K,V>> i = entrySet().iterator();
Entry<K,V> correctEntry = null;
//找到第一个key相等的,找到后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;
//使用迭代器删除该entry
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());
}
//调用entrySet的清空方法
public void clear() {
entrySet().clear();
}
transient Set<K> keySet;
transient Collection<V> values;
//获取key集合
public Set<K> keySet() {
Set<K> ks = keySet;
if (ks == null) {
ks = new AbstractSet<K>() {
//实现迭代器
public Iterator<K> iterator() {
return new Iterator<K>() {
//使用entrySet的迭代器
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);
}
};
keySet = ks;
}
return ks;
}
//
public Collection<V> values() {
Collection<V> vals = values;
if (vals == null) {
vals = new AbstractCollection<V>() {
public Iterator<V> iterator() {
return new Iterator<V>() {
//依然使用entrySet的迭代器,但是
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);
}
};
values = vals;
}
return vals;
}
public abstract Set<Entry<K,V>> entrySet();
//判断map是否与o等价
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Map))
return false;
Map<?,?> m = (Map<?,?>) o;
if (m.size() != size())
return false;
try {
//获取entrySet的迭代器
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext()) {
Entry<K,V> e = i.next();
//获取元素的key,value
K key = e.getKey();
V value = e.getValue();
/*若value为null,则只有包含key并且对应value为null才相等,
有一个不等则直接返回false
若value不为null,则必须对应的value等价
*/
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<Entry<K,V>> i = entrySet().iterator();
//每个entry的hash值相加
while (i.hasNext())
h += i.next().hashCode();
return h;
}
//转为{key=value, ...}形式
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(' ');
}
}
//克隆
protected Object clone() throws CloneNotSupportedException {
AbstractMap<?,?> result = (AbstractMap<?,?>)super.clone();
result.keySet = null;
result.values = null;
return result;
}
//两者都为null,或者等价时相等
private static boolean eq(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
}
内部类SimpleEntry
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;
}
//根据entry构造
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) {
//是Entry类型
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
//调用AbstractMap的eq方法
return eq(key, e.getKey()) && eq(value, e.getValue());
}
//hash值为key跟value异或的结果
public int hashCode() {
return (key == null ? 0 : key.hashCode()) ^
(value == null ? 0 : value.hashCode());
}
public String toString() {
return key + "=" + value;
}
}
内部类SimpleImmutableEntry
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;
}
//相比于SimpleEntry,不支持修改值
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());
}
public String toString() {
return key + "=" + value;
}
}
以上源码总体来说还是比较简单的,在迭代的时候,都是采用entrySet的迭代器形式,但是entrySet是交给子类来实现的。
Immutable是不可修改的意思,因此将setValue方法采用不支持的方式,抛出异常。