Java源码阅读之AbstractMap

本文深入解析了Java中AbstractMap类的实现细节,包括其构造函数、主要方法如size、isEmpty、containsKey等的实现原理,以及内部如何通过迭代器遍历键值对。同时,文章还介绍了AbstractMap如何提供键、值和键值对的视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java源码阅读之AbstractMap

类定义

public class AbstractMap<K,V> implements Map<K,V>{}

方法

// 返回Map中键值对的数量,如果键值对的数量大于Integer.MAX_VALUE则返回Integer.MAX_VALUE;
public int size(){
	return entrySet().size();
}

// 如果Map中键值对的数量为0,则返回true
public boolean isEmpty(){
	return size() == 0;
}

// 如果map中包含指定的键则返回true
public boolean containsKey(Object key){
	Iterator<Entry<K,V>> it = entrySet().iterator();
	if(value == null){
		while(it.hasNext()){
			Entry<K,V> e = i.next();
			if(e.getKey() == null)
				return true;
		}
	}else{
		while(it.hasNext()){
			Entry<K,V> e = i.next();
			if(key.equals(e.getKey()))
				return true;
		}
	}
	return false;
}

// 如果map中包含指定的值则返回true
public boolean containsValue(Object value){
	Iterator<Entry<K, V>> it = keySet().iterator();
	if(value == null){
		while(it.hasNext()){
			Entry<K,V> e = it.next();
			if(e.getValue() == null)
				return true;
		}
	}else{
		while(it.hasNext()){
			Entry<K,V> e = i.next();
			if(value.equals(e.getValue()))
				return true;
		}
	}
	return false;
}

// 返回map中键k所关联的值,如果map中没有键k则返回null
public V get(Object k){
	Iterator<Entry<K,V>> it = entrySet().iterator();
	if(k == null){
		while(it.hasNext()){
			Entry<K,V> e = i.next();
			if(e.getKey() == null)
				return e.getValue();
		}
	}else{
		while(it.hasNext()){
			Entry<K,V> e = i.next();
			if(k.equals(e.getKey()))
				return e.getValue();
		}
	}
	return null;
}

// 向map中插入键值对,如果已经存在键key则对原value值进行更新
// 如果已经存在key则返回原来的value值,否则返回null
public V put(K key, V value){
	// 该方法必须由子类实现,否则就不能使用put操作
	throw new UnSupportOperationException();
}

// 删除指定的键值对
// 如果map中存在键key,则返回其关联的值否则返回null
public V remove(K key){
	Iterator<Entry<K,V>> it = entrySet().iterator();
	Entry<K,V> currentEntry = null;
	if(key == null){
		while(currentEntry == null && it.hasNext()){
			Entry<K, V> e = it.next();
			if(e.getKey() == null)
				currentEntry = e;
		}
	}else{
		while(currentEntry == null && it.hasNext()){
			Entry<K,V> e = it.next();
			if(key.equals(e.getKey()))
				currentEntry = e;
		}
	}
	V oldValue = null;
	if(currentEntry != null){
		oldValue = currentEntry.getValue();
		// 移除上一次返回的键值对,也就是目标键值对
		it.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());
	}
}

// 删除map中的所有键值对
public void clear(){
	entrySet().clear();
}


transient Set<K> keySet;
// 返回map中所有键的一个视图
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 o){
				return AbstractMap.this.containsKey(o);
			}
		};
		keySet = ks;
	}
	return ks;
}

transient Collection<V> values;
// 返回map中所有值的一个视图
public Collection<V> values(){
	Collection<V> vals = values;
	if(vals == null){
		vals = new AbstractCollection<V>(){
			public Iterator<V> iterator(){
				return new Iterator<V>(){
					private final Iterator<Entry<K,V>> i = keySet().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 o){
				return AbstractMap.this.containsValue(o);
			}
		};
		values = vals;
	}
	return vals; 
}

// 返回map中所有键值对的一个视图
public abstract Set<Entry<K,V>> entrySet();

public boolean equals(Object o){
	if(o == this){
		return true;
	}
	if(!(o instanceof Map)){
		return false;
	}
	Map<?, ?> m = (Map<?,?>)o;
	if(m.size() != this.size()){
		return false;
	}
	try{
		Iterator<Entry<K,V>> it = entrySet().iterator();
		while(it.hasNext()){
			Entry<K,V> e = it.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(Exception e){
		return false;
	}
	return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值