Java集合Map-HashTable
一、简介
1)基于哈希表实现的,是线程安全的map集合,可以用于多线程环境,但是效率比较低下。Value不能为空。
2)HashTable实现了Serializable接口,支持序列化,实现了Cloneable,Map接口。同时也继承了Dictionary父类。
3)当table的实际数量count超过分配容量的0.75时,新建一个Entry[]是原先的两倍,并且重新Hash(rehash)
二、属性
private transient Entry<?,?>[] table;
//tabled的键值对个数
private transient int count;
//阈值,用于判断是否需要HashTable容量
private int threshold;
//加载因子默认是0.75
private float loadFactor;
//HashTable被改变的次数
private transient int modCount = 0;
//序列化版本
rivate static final long serialVersionUID = 1421746759512286392L;
Entry类
private static class Entry<K,V> implements Map.Entry<K,V> {
int hash;
K key;
V value;
// 指向的下一个Entry,即链表的下一个节点
Entry<K,V> next;
// 构造函数
protected Entry(int hash, K key, V value, Entry<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
protected Object clone() {
return new Entry<K,V>(hash, key, value,
(next==null ? null : (Entry<K,V>) next.clone()));
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
// 设置value。若value是null,则抛出异常。
public V setValue(V value) {
if (value == null)
throw new NullPointerException();
V oldValue = this.value;
this.value = value;
return oldValue;
}
// 覆盖equals()方法,判断两个Entry是否相等。
// 若两个Entry的key和value都相等,则认为它们相等。
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
(value==null ? e.getValue()==null : value.equals(e.getValue()));
}
public int hashCode() {
return hash ^ (value==null ? 0 : value.hashCode());
}
public String toString() {
return key.toString()+"="+value.toString();
}
}
三、方法
1.synchronzied V put(K key, V value)
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 = key.hashCode(); //计算key的hashCode值
int index = (hash & 0x7FFFFFFF) % tab.length; //获取keyd的hashCode后获得非符号位的数值
@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)) { //如果存在相同的hash值和key,那么将替换旧的值
V old = entry.value;
entry.value = value;
return old; //返回
}
}
addEntry(hash, key, value, index); //不然则添加一个entry
return null;
}
private void addEntry(int hash, K key, V value, int index) {
modCount++; //修改次数加一
Entry<?,?> tab[] = table;
if (count >= threshold) { //判断当先table的数量是否大于阈值
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
hash = key.hashCode();//重新计算后的hashCode
index = (hash & 0x7FFFFFFF) % tab.length; //计算key的index位置
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>) tab[index]; //将index位置的Entry保存
tab[index] = new Entry<>(hash, key, value, e); //创建一个新的Entry节点,并添加到table中
count++; //数量加一
}
@SuppressWarnings("unchecked")
protected void rehash() {
int oldCapacity = table.length;
Entry<?,?>[] oldMap = table;//将原先的table,创建给一个数组
// overflow-conscious code
int newCapacity = (oldCapacity << 1) + 1; //新数组容量扩充为2倍+1
if (newCapacity - MAX_ARRAY_SIZE > 0) { /判断不能大于MAX_ARRAY_SIZE值
if (oldCapacity == MAX_ARRAY_SIZE)
// Keep running with MAX_ARRAY_SIZE buckets
return;
newCapacity = MAX_ARRAY_SIZE;
}
Entry<?,?>[] newMap = new Entry<?,?>[newCapacity]; //创建一个newCapacity大小的Entry数组
modCount++;
threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
table = newMap;
for (int i = oldCapacity ; i-- > 0 ;) { //遍历旧的数组,将数据复制到新的数组里面,并且重新计算index和hashCode
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;
}
}
}
2.synchrozied V get(Object key)
public synchronized V get(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();//计算key的hashCode和index索引值
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) { //找出key对应的Entry链表中hash和key都相等的元素
return (V)e.value;
}
}
return null;
}
3.contains/containsKey/containsValue实现原理基本相同
public synchronized boolean contains(Object value) {
if (value == null) {
throw new NullPointerException();
}
Entry<?,?> tab[] = table;
for (int i = tab.length ; i-- > 0 ;) {
for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) {
if (e.value.equals(value)) { //判断value值是否相同
return true;
}
}
}
return false;
}
public synchronized boolean containsKey(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return true;
}
}
return false;
}