package java.util;
import java.util.Map.Entry;
import sun.net.www.content.image.gif;
// 抽象类,实现Map集合,hashMapp, hashTable均实现的这个类
public abstract class AbstractMap<K,V> implements Map<K,V> {
protected AbstractMap() {
}
// 计算当前map的大小
public int size() {
return entrySet().size();
}
// 判断当前map是否为空
public boolean isEmpty() {
return size() == 0;
}
// 判断当前的map中是否包含传入的value
public boolean containsValue(Object value) {
// 定义一个迭代器
Iterator<Entry<K,V>> i = entrySet().iterator();
// 如value为空,就查找是否有为空value
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();
// 不为空通过equals比较,否则value.equals会报错
if (value.equals(e.getValue()))
return true;
}
}
return false;
}
// 判断当前的map中是否包含传入 的key
public boolean containsKey(Object key) {
// 获取当前map的迭代器
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获取到对应的value数据
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移除当前map中的键值对
public V remove(Object key) {
// 获取到当前map的迭代器
Iterator<Entry<K,V>> i = entrySet().iterator();
Entry<K,V> correctEntry = null;
// 记录下需要移除的entry,在循环时不能进行移除,
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) {
// entry不为空则进行移除
oldValue = correctEntry.getValue();
i.remove();
}
// 将移除的value的值返回
return oldValue;
}
// 向当前的map中放入一个map
public void putAll(Map<? extends K, ? extends V> m) {
// 对传入 的map进行循环,将数据不断的放入当前的map中
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
put(e.getKey(), e.getValue());
}
// 清除当前map的数据
public void clear() {
entrySet().clear();
}
// transient将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会被序列化
// 将所有的key存入set集合中
transient Set<K> keySet;
// 将所有的value存入collection集合中
transient Collection<V> values;
// 定义一个keySet
public Set<K> keySet() {
Set<K> ks = keySet;
// keySet中不存入值,则进行下面的操作, 即 这个方法只会进行一次初始化
if (ks == null) {
// 定义一个AbstractSet
// AbstractSet是抽象类,new时需要重新实现其中的方法
// 将其中的方法换成当前类的方法
ks = new AbstractSet<K>() {
// 重新定义iterator
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();
}
};
}
// 重新定义size()
public int size() {
return AbstractMap.this.size();
}
// 重新定义isEmpty()
public boolean isEmpty() {
return AbstractMap.this.isEmpty();
}
// 重新定义clear
public void clear() {
AbstractMap.this.clear();
}
// 重新定义contains
public boolean contains(Object k) {
return AbstractMap.this.containsKey(k);
}
};
keySet = ks;
}
return ks;
}
// 定义一个values,与上面基本一样
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;
}
// 用来存放键值对
public abstract Set<Entry<K,V>> entrySet();
// 用来进行比较两个map
public boolean equals(Object o) {
if (o == this)
return true;
// 判断传入的是否是Map,不是直接返回false
if (!(o instanceof Map))
return false;
// 是map类,就进行强转为当前的类型
Map<?,?> m = (Map<?,?>) o;
// 判断map的长度是否相等
if (m.size() != size())
return false;
try {
// 判断map中的每一个键值对是否相等
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的hashCode值
public int hashCode() {
int h = 0;
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return h;
}
// 重写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(' ');
}
}
// AbstractMap实例的浅拷贝:键和值本身不被克隆。
protected Object clone() throws CloneNotSupportedException {
AbstractMap<?,?> result = (AbstractMap<?,?>)super.clone();
result.keySet = null;
result.values = null;
return result;
}
// 比较传入的两个map
private static boolean eq(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
// 保存密钥和值的条目。 该值可以使用setValue方法更改。 这个类促进了构建自定义map实现的过程。 例如,在方法Map.entrySet().toArray中返回SimpleEntry个实例的数组可能很方便 。
public static class SimpleEntry<K,V> implements Entry<K,V>, java.io.Serializable
{
private static final long serialVersionUID = -8499721149061103585L;
// 保存key
private final K key;
// 保存value
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();
}
// 获取到当前的key
public K getKey() {
return key;
}
// 获取至当前的value
public V getValue() {
return value;
}
// 重新设置value的值, 并将旧的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());
}
// 计算hashCode
public int hashCode() {
return (key == null ? 0 : key.hashCode()) ^
(value == null ? 0 : value.hashCode());
}
// 重新定义toString
public String toString() {
return key + "=" + value;
}
}
// 保持一个不变的钥匙和价值的条目。 此类不支持方法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) {
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;
}
}
}