第一次进行源码分析
不多逼逼,直接上代码截图
1.继承实现结构
继承实现体系
关于Dictionary类
Dictionary,字典的意思,就是一个键值对的列表,通过键能够拿到值,类似map
2.put()方法分析
put()方法分析
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
Make sure the value is not null
Makes sure the key is not already in the hashtable.确保key唯一
3.总结
就只分析了继承体系和put()方法,但已经达到我想要的结果了
继承抽象类Dictionary,想象成数据字典
实现Map,克隆,序列化,让其有该有的功能
put()方法前加关键字synchronized,说明HashTable加锁了,是线程安全的
对传入进来的value参数做了null 的判断,说明HashTable中的value值是不能为null的
HashTable,线程安全的容器,value值不能为null,具有数据字典的特征,以及其他map容器的特点
其他的再分析吧,TaTa~