Java底层类和源码分析系列-HashTable底层架构和源码分析
2021-02-20 18:20
标签:hashcode 并发 checked public serial put element ext dde ConcurrentModificationException异常,每一次修改hashtable中的数据都更新modCount的值,而迭代器Enumerator 可以看出,每次插入都要遍历一次链表,每次都是O(n),效率比HashMap要低下,HashMap一般为红黑树内O(LogN),链表内O(n),直接定位则为1 Java底层类和源码分析系列-HashTable底层架构和源码分析 标签:hashcode 并发 checked public serial put element ext dde 原文地址:https://www.cnblogs.com/starcrm/p/12678770.html几个要点
类定义
public class Hashtable
属性
构造器
public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity )
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
if (loadFactor Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load: "+loadFactor);
if (initialCapacity==0)
initialCapacity = 1;
this.loadFactor = loadFactor;
table = new Entry,?>[initialCapacity];
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
}
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
public Hashtable() {
this(11, 0.75f);
}
public Hashtable(Map extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f);
putAll(t);
}
modCount一致性
public static void main(String[] args) {
Hashtable
public T next() {
//首先判断modCount和expectedModCount是否相等
//由于在主程序中Hashtable对象通过tb.remove()方法修改了modCount的值,使得expectedModCount和modCount不相等而抛出异常
//解决办法就是将tb.remove()方法替换为iter.remove()方法
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
return nextElement();
}
//该方法在remove元素的同时修改了modCount和expectedModCount的值
public void remove() {
if (!iterator)
throw new UnsupportedOperationException();
if (lastReturned == null)
throw new IllegalStateException("Hashtable Enumerator");
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
synchronized(Hashtable.this) {
Entry,?>[] tab = Hashtable.this.table;
int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry主要方法
put
public synchronized V put(K key, V value) {
//值不允许为null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry,?> tab[] = table;
//得到键的hash
int hash = key.hashCode();
//得到对应hash在数组中的桶索引
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
//得到桶中链表头节点
Entry
addEntry
private void addEntry(int hash, K key, V value, int index) {
modCount++;
Entry,?> tab[] = table;
//如果尺寸超过了阈值,进行rehash
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
hash = key.hashCode();
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry
get
public synchronized V get(Object key) {
Entry,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
//遍历所有元素,哈希值和key一致,则返回
for (Entry,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return (V)e.value;
}
}
return null;
}
文章标题:Java底层类和源码分析系列-HashTable底层架构和源码分析
文章链接:http://soscw.com/index.php/essay/58097.html