参考博文:https://blog.csdn.net/dingjianmin/article/details/79774192
继承关系
继承了Dictionary抽象类
参数:
大小:count
容量:默认11(>=1的任何整数)
负载因子:0.75
扩容阈值:容量*负载因子(或是两倍的同映射Map的size)
数据结构实现:
- 数组(一个Entry类型的Table)
Entry<K,V> implements Map.Entry<K,V>
线程安全性
线程安全
插入过程
- 不允许插入空键或者空值
- 链地址法
- 同key覆盖
扩容机制
每次都扩容为原来2倍+1
Hashtable的构造方法
①指定初始容量和负载因子
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 = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
}
②指定初始容量
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
③无参构造
public Hashtable() {
this(11, 0.75f);
}
④构造一个与给定的 Map 具有相同映射关系的新哈希表
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f);
putAll(t);
}
被synchronized关键字修饰的方法
size(),isEmpty(),Enumeration keys(),Enumeration elements(),contains(),containsKey(),get(),put(),remove()2个,putAll(),clear(),clone(),toString(),equals(),hashCode(),getOrDefault(),forEach(),replaceAll(),putIfAbsent(),replace()2个,computeIfAbsent()2个,compute(),merge()
被volatile关键字修饰的变量
/**
* Each of these fields are initialized to contain an instance of the
* appropriate view the first time this view is requested. The views are
* stateless, so there's no reason to create more than one of each.
*/
private transient volatile Set<K> keySet;
private transient volatile Set<Map.Entry<K,V>> entrySet;
private transient volatile Collection<V> values;