//此类提供 Map接口的骨干实现,以最大限度地减少实现此接口所需的工作
public abstract class AbstractMap<K,V> implements Map<K,V> {
protected AbstractMap() {//构造方法
}
public int size() {//返回此映射中的键-值映射关系数
return entrySet().size();
}
public boolean isEmpty() {//如果此映射未包含键-值映射关系,则返回 true
return size() == 0;
}
public boolean containsValue(Object value) {//如果此映射将一个或多个键映射到指定值,则返回 true
Iterator<Entry<K,V>> i = entrySet().iterator();//获取entrySet迭代器
if (value==null) {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (e.getValue()==null)//从entrySet中获取value进行比较
return true;
}
} else {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (value.equals(e.getValue()))//从entrySet中获取value进行比较
return true;
}
}
return false;
}
public boolean containsKey(Object key) {//如果此映射包含指定键的映射关系,则返回 true
Iterator<Map.Entry<K,V>> i = entrySet().iterator();//获取entrySet迭代器
if (key==null) {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (e.getKey()==null)//从entrySet中获取key进行比较
return true;
}
} else {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (key.equals(e.getKey()))//从entrySet中获取key进行比较
return true;
}
}
return false;
}
public V get(Object key) {//返回指定键所映射的值
Iterator<Entry<K,V>> i = entrySet().iterator();//获取entrySet迭代器
if (key==null) {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (e.getKey()==null)//从entrySet中获取key进行比较
return e.getValue();//从entrySet中获取值
}
} else {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (key.equals(e.getKey()))//从entrySet中获取key进行比较
return e.getValue();//从entrySet中获取值
}
}
return null;
}
public V put(K key, V value) {//将指定的值与此映射中的指定键关联。始终抛出异常
throw new UnsupportedOperationException();
}
public V remove(Object key) {//如果存在一个键的映射关系,则将其从此映射中移除
Iterator<Entry<K,V>> i = entrySet().iterator();//获取entrySet迭代器
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();//以前与 key 关联的值;如果没有 key 的映射关系,则返回 null
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() {//键的 Set视图,修改该set或与原键值对相互影响
if (keySet == null) {
keySet = new AbstractSet<K>() {//匿名内部类
public Iterator<K> iterator() {
return new Iterator<K>() {
private Iterator<Entry<K,V>> i = entrySet().iterator();//获取Entry<K,V>迭代器
//迭代器的内部实现
public boolean hasNext() {
return i.hasNext();
}
public K next() {
return i.next().getKey();
}
public void remove() {
i.remove();
}
};
}
public int size() {//直接调用外部的size方法
return AbstractMap.this.size();
}
public boolean isEmpty() {//直接调用外部的isEmpty方法
return AbstractMap.this.isEmpty();
}
public void clear() {//直接调用外部的clear方法
AbstractMap.this.clear();
}
public boolean contains(Object k) {//直接调用外部的containsValue方法
return AbstractMap.this.containsKey(k);
}
};
}
return keySet;
}
public Collection<V> values() {//返回此映射中包含的值的 Collection 视图
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() {//直接调用外部的size方法
return AbstractMap.this.size();
}
public boolean isEmpty() {//直接调用外部的isEmpty方法
return AbstractMap.this.isEmpty();
}
public void clear() {//直接调用外部的clear方法
AbstractMap.this.clear();
}
public boolean contains(Object v) {//直接调用外部的containsValue方法
return AbstractMap.this.containsValue(v);
}
};
}
return values;
}
public abstract Set<Entry<K,V>> entrySet();//返回此映射中包含的映射关系的 Set视图,修改时与map会互相影响
public boolean equals(Object o) {//比较指定对象与此映射的相等性
if (o == this)//引用相等必然相等
return true;
if (!(o instanceof Map))//必须是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();//获取Entry<K,V>
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)))//value不为null时的键值对的比较
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();
while (i.hasNext())
h += i.next().hashCode();
return h;
}
public String toString() {//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<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);
}
//静态类、内部类。Entry<K,V>接口的实现
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() {//返回此映射项的 String 表示形式
return key + "=" + value;
}
}
//静态类,Entry<K,V>接口的实现 。此类不支持 setValue方法。维护不可变的键值。方法同上
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) {//不支持setValue操作
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;
}
}
}