AbstractMap
1.AbstractMap声明
public abstract class AbstractMap<K,V> implements Map<K,V> {}
AbstractMap实现Map接口,主要是对Map接口中的抽象方法进行重写,个别方法因为子类的不同特性在子类中进行具体实现实现。
- map接口定义了一些对于map的基本操作方法。
2.抽象函数entrySet()
public abstract Set<Entry<K,V>> entrySet();
AbstractMap类中有一个唯一的抽象函数 entrySet() ,类中对集合视图操作的很多方法都是依赖这个抽象函数的,它返回一个保存所有 key-value 映射的Set。
3.size()
//获取map的大小也就是set的大小。
public int size() {
return entrySet().size();
}
//判断map的大小也就是set的大小是否等于0。
public boolean isEmpty() {
return size() == 0;
}
4.containsValue(Object value)
//判断map中是否包含某个对象
public boolean containsValue(Object value) {
//获取set的迭代器
Iterator<Entry<K,V>> i = entrySet().iterator();
//如果为空,迭代到为null的值返回true
if (value==null) {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (e.getValue()==null)
return true;
}
} else {
//如果为空,匹配到值返回true
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (value.equals(e.getValue()))
return true;
}
}
//否则返回flase
return false;
}
//判断map中是否包含某个key
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;
}
5.get(Object key)
//根据key使用迭代器获取key对象的值,否则返回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)
return e.getValue();
}
} else {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (key.equals(e.getKey()))
return e.getValue();
}
}
return null;
}
6.put(K key, V value)
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends K, ? extends V> m) {
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
put(e.getKey(), e.getValue());
}
可以看到put方法直接抛出异常。
所以当我们要实现一个不可变的 Map 时,只需要继承 AbstractMap 类并实现 entrySet() 即可。
如果想要实现一个可变的 Map ,我们还需要重写 put() 方法,因为 AbstractMap 类中默认不支持 put实现,子类必须重写该方法的实现,否则会抛出异常:
7.remove(Object key)
//遍历entrySet,先找到对应的key的entry,然后删除。
public V remove(Object key) {
Iterator<Entry<K,V>> i = entrySet().iterator();
Entry<K,V> correctEntry = null;
//遍历查找,当某个 Entry 的 key 和 指定 key 一致时结束
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();
//调用迭代器的 remove 方法
i.remove();
}
return oldValue;
}
//清空entrySet,等价于清空该Map。
public void clear() {
entrySet().clear();
}
remove方法和put方法是一样的,如果是可变集合的话,需要重写。
8.keySet()和values()
// 返回一个AbstractSet的实现,包含了所有的key
public Set<K> keySet() {
Set<K> ks = keySet;
if (ks == null) {
ks = 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);
}
};
keySet = ks;
}
return ks;
}
// 返回一个AbstractCollection的实现,包含了所有的value
public Collection<V> values() {
Collection<V> vals = values;
if (vals == null) {
vals = 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);
}
};
values = vals;
}
return vals;
}
AbstractMap没有提供 entrySet() 的实现,但是却提供了 keySet() 与 values() 集合视图的默认实现,它们都是依赖于 entrySet() 返回的集合视图实现的。
9.equals(Object o)
public boolean equals(Object o) {
//如果对比自己直接返回true
if (o == this)
return true;
//如果不是mao类型的对象,直接返回flase
if (!(o instanceof Map))
return false;
//对象强转为map
Map<?,?> m = (Map<?,?>) o;
//如果大小不一样直接返回flase
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) {
//如果循环到null值,判断对比map不存在key或者其key对应的值不为null,返回flase,这里多一个key是否存在的判断,因为key不存在会返回null,判断不了null值。
if (!(m.get(key)==null && m.containsKey(key)))
return false;
} else {
//循环的值不等于对比map,key对应的值,返回flase。
if (!value.equals(m.get(key)))
return false;
}
}
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
//对比完成返回true
return true;
}
重写了equals方法,判断两个ma值是否相等,key相等,并且key对应的值相等即为相等。
10.hashCode()
public int hashCode() {
int h = 0;
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return h;
}
重写了hashCode()方法,返回map中每个数据的hashCode()相加的值。
11.toString()
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(' ');
}
}
把map转换为string输出,可以看到是把key和value用=拼接,每个key之间用,分割,前后加上{}。平时打印map的时候就是这样的格式了,也可以把这样的格式转换为map。
12.SimpleEntry<K,V>和SimpleImmutableEntry<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() {
return key + "=" + value;
}
}
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());
}
public String toString() {
return key + "=" + value;
}
}
除了上面的方法之外,AbstractMap 类中还提供了两个子类,分别是 SimpleEntry,SimpleImmutableEntry,两个子类都实现了Map.Entry 以及 Serializable 接口。
两个实现都非常简单,具体的方法也是大同小异,唯一有区别的是 setValue 这个方法,SimpleEntry 支持 setValue 的操作实现,而 SimpleImmutableEntry 就没有实现,说明前者为可变集合,后者为不可变集合。
13.总结
- AbsrtactMap是其他Map具体实现类的骨架实现。
- AbstractMap实现Map接口,主要是对Map接口中的抽象方法进行重写,个别方法因为子类的不同特性在子类中进行具体实现实现。
- 在AbstractMap中,只没有对put()方法进行重写。
- 在AbstractMap中,提供了两种Entry,可变Entry和不可变Entry,这两种均实现Map.Entry接口, SimpleEntry和SimpleImmutableEntry,SimpleEntry是中key变量不可变,SimpleImmutableEntry的key,value变量均不可变(final修饰),两者的equals()和hashcode()实现相同。 ps:我也是在自己学习和整理中,只是做个随笔,如有错误或者遗漏欢迎大家指出,或者一起讨论,一起进步。