为什么80%的码农都做不了架构师?>>>
1、Hash链表结构
详见:HashMap-Hash链表结构
2、HashTable结构简述
HashTable不仅仅继承于Map接口实现了Map的主结构Entry,而且还是Dictionary的子类。
private static class Entry implements Map.Entry {int hash;final K key;V value;Entry next;protected Entry(int hash, K key, V value, Entry next) {this.hash = hash;this.key = key;this.value = value;this.next = next;}public V setValue(V value) {//注意,HashTable的value不可为空if (value == null)throw new NullPointerException();V oldValue = this.value;this.value = value;return oldValue;}}
HashTable的内部类Entry,包括了hash值、key、value和指向下一个Entry的引用。需要注意的是value不能为空。
public class Hashtableextends Dictionaryimplements Map, Cloneable, java.io.Serializableprivate transient Entry[] table; //存储键值对的数组private transient int count; //table中存储的键值对总数private int threshold; //阈值private float loadFactor; //负载因子private transient int modCount = 0; //用于快速失败/*** 无参构造函数,hashtable的初始化容量是11,负载因子是0.75*/public Hashtable() {this(11, 0.75f);} /*** @param initialCapacity hashtable的初始化容量.*/public Hashtable(int initialCapacity) {this(initialCapacity, 0.75f);}/*** @param initialCapacity hashtable的初始化容量.* @param loadFactor 负载因子.*/public Hashtable(int initialCapacity, float loadFactor) {this.loadFactor = loadFactor;table = new Entry[initialCapacity];threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);//初始化hash种子initHashSeedAsNeeded(initialCapacity);}public Hashtable(Map extends K, ? extends V> t) {this(Math.max(2*t.size(), 11), 0.75f);putAll(t);}}
3、HashTable详解
3.1)关于初始化
jdk1.7中给HashTable也提供了4个构造方法,四个方法的唯一差别是对参数initialCapacity、loadFactor,提供了不同的默认值。最终还是调用initHashSeedAsNeeded方法进行hash种子初始化
3.2)关于写操作
3.3)关于读操作
3.4)关于扩容