java 中hash表的用法(集合篇-HashtableJAVA)
Hashtable也是Map接口的一种实现,也是key-value形式存在,不过key和value都不能为null,且key必须实现hashCode方法;在性能和空间考虑,和HashMap类一样用加载因子和初始容量。且是线程安全的;底层是用数组 链表实现。
1、new Hashtable<>();
public Hashtable() {
this(11, 0.75f);
}
//initialCapacity = 11,loadFactor = 0.75f
public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "
initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load: " loadFactor);
if (initialCapacity==0)
initialCapacity = 1;
this.loadFactor = loadFactor;
//定义了数组
table = new Entry<?,?>[initialCapacity];
//threshold = 11 * 0.75 = 8.25
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE 1);
}
2、java.util.Hashtable#put
// 为啥说Hashtable是线程安全的,主要是因为使用synchronized关键字
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;
//这里就说明了为啥key需要实现hashCode方法
int hash = key.hashCode();
// hash值算出数组下标
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;
}
}
// 如果找不到值就添加entry条目
addEntry(hash, key, value, index);
return null;
}
//添加条目
private void addEntry(int hash, K key, V value, int index) {
Entry<?,?> tab[] = table;
//如果count>=阈值后,就需要扩容
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
//重新hash且扩容
rehash();
tab = table;
hash = key.hashCode();
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>) tab[index];
//这里形成链表
tab[index] = new Entry<>(hash, key, value, e);
count ;
modCount ;
}
//重新hash且扩容
protected void rehash() {
int oldCapacity = table.length;
Entry<?,?>[] oldMap = table;
// overflow-conscious code
// newCapacity = 2 * oldCapacity 1
int newCapacity = (oldCapacity << 1) 1;
if (newCapacity - MAX_ARRAY_SIZE > 0) {
if (oldCapacity == MAX_ARRAY_SIZE)
// Keep running with MAX_ARRAY_SIZE buckets
return;
newCapacity = MAX_ARRAY_SIZE;
}
Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];
modCount ;
threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE 1);
table = newMap;
//将旧的数组数据重新根据新的数组长度hash算出来的数组下标放到新的数组中
for (int i = oldCapacity ; i-- > 0 ;) {
for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
Entry<K,V> e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = (Entry<K,V>)newMap[index];
newMap[index] = e;
}
}
}
3、java.util.Hashtable#remove(java.lang.Object)
// 根据key移除元素
public synchronized V remove(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
//hash找到数组下标
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
//遍历查找元素。
for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
//查到元素
if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
modCount ;
count--;
V oldValue = e.value;
//帮助GC回收
e.value = null;
return oldValue;
}
}
return null;
}
其实Hashtable的底层实现挺简单的;线程安全从上面的源码分析中可以知道,通过synchronized关键字来保证线程安全的。且实现是通过数组 链表。
喜欢这样做源码解析的同学,可以点赞、关注 收藏,后期还会推出其他框架源码分析。
如果有补充的地方,留言区见。
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com