java之--HashTable
===========================================
和HashMap相似,数组+链表结构
final K key;
V value;
Entry<K,V> next;
}
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, Serializable {
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
1.HashMap是非synchronized,而HashTable是synchronized
HashMap:public V put(K key, V value) {}
HashTable:public synchronized V put(K key, V value) {}
2.HashMap可以接受为null的键(key)和值(value),HashTable键值都不能为null
HashMap:
但任意数量的条目可以是空的value。
HashTable:
===========================================
和HashMap相似,数组+链表结构
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable {
//The hash table data.
private transient Entry<K,V>[] table;//数据存储区
//The total number of entries in the hash table.
private transient int count;//table中的数据个数
//The table is rehashed when its size exceeds(超过) this threshold
//The value of this field is (int)(capacity * loadFactor) capacity为table的容量
private int threshold;//扩容阈值
//The load factor for the hashtable
private float loadFactor;//扩容负载因子
如果容量table.length=capacity=16,
负载因子loadFactor=0.75,
则阈值threshold=16 * 0.75 = 12
也就是容器中有12个元素的时候,容器开始扩容
//The number of times this Hashtable has been structurally modified
private transient int modCount = 0;
//hashSeed用于计算key的hash值,它与key的hashCode进行按位异或运算。
//这个hashSeed是一个与实例相关的随机值,主要用于解决hash冲突。
transient int hashSeed;
}
初始化:
public Hashtable() {
this(11, 0.75f);//默认初始容量为11,负载因子为0.75
}
public Hashtable(int initialCapacity, float loadFactor) {
......
if (initialCapacity==0)
initialCapacity = 1;
this.loadFactor = loadFactor;//负载因子
table = new Entry[initialCapacity];//初始化table
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);//计算阀值
initHashSeedAsNeeded(initialCapacity);//初始化HashSeed值
}
1.put()添加元素
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {//value不能为空
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;//有重复的key,新value会覆盖原来的
return old;
}
}
modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();//扩容
tab = table;
hash = hash(key);
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
Entry<K,V> e = tab[index];
//如果有碰撞,hash冲突,新添加的元素放在链头,原来的元素为新添加元素的next
tab[index] = new Entry<>(hash, key, value, e);
count++;
return null;
}
Entry节点:
private static class Entry<K,V> implements Map.Entry<K,V> {
int hash;final K key;
V value;
Entry<K,V> next;
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
//扩容
protected void rehash() {
int oldCapacity = table.length;
Entry<K,V>[] oldMap = table;
// overflow-conscious code
int newCapacity = (oldCapacity << 1) + 1;//新容量为oldCapacity*2 + 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<K,V>[] newMap = new Entry[newCapacity];
modCount++;
threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
boolean rehash = initHashSeedAsNeeded(newCapacity);
table = newMap;
//容量增加后,重新调整元素位置
for (int i = oldCapacity ; i-- > 0 ;) {//桶(数组)
for (Entry<K,V> old = oldMap[i] ; old != null ; ) {//链(链表)
Entry<K,V> e = old;
old = old.next;
if (rehash) {
e.hash = hash(e.key);
}
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newMap[index];
newMap[index] = e;
}
}
}
2.remove()删除元素
public synchronized V remove(Object key) {
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
modCount++;
if (prev != null) {//在链上找到
prev.next = e.next;
} else {
tab[index] = e.next;//在桶上找到
}
count--;
V oldValue = e.value;
e.value = null;
return oldValue;
}
}
return null;
}
HashTable与HashMap不同点:
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, Serializable {
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
1.HashMap是非synchronized,而HashTable是synchronized
HashMap:public V put(K key, V value) {}
HashTable:public synchronized V put(K key, V value) {}
2.HashMap可以接受为null的键(key)和值(value),HashTable键值都不能为null
HashMap:
public V put(K key, V value) {
......
if (key == null)
return putForNullKey(value);//放在table[0]或其链上
}
HashMap中只有一条记录可以是一个空的key(多个时,新value会覆盖原来的),
但任意数量的条目可以是空的value。
HashTable:
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
int hash = hash(key);
......
}
private int hash(Object k) {
return hashSeed ^ k.hashCode();//key=k为null,这里会报NullPointerException
}
HashTable的key、value都不能为空