Hashtable 的实现原理

概述

和 HashMap 一样,Hashtable 也是一个散列表,它存储的内容是键值对。
Hashtable 在 Java 中的定义为:

public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable{}

从源码中,我们可以看出,Hashtable 继承于 Dictionary 类,实现了 Map, Cloneable, java.io.Serializable接口。其中Dictionary类是任何可将键映射到相应值的类(如 Hashtable)的抽象父类,每个键和值都是对象;
Hashtable 源码解读
成员变量
Hashtable是通过"拉链法"实现的哈希表。它包括几个重要的成员变量:table, count, threshold, loadFactor, modCount。
table是一个 Entry[] 数组类型,而 Entry(在 HashMap 中有讲解过)实际上就是一个单向链表。哈希表的"key-value键值对"都是存储在Entry数组中的。
count: 是 Hashtable 的大小,它是 Hashtable 保存的键值对的数量。
threshold: 是 Hashtable 的阈值,用于判断是否需要调整 Hashtable 的容量。threshold 的值="容量*加载因子"。
loadFactor: 就是加载因子。
modCount: 是用来实现 fail-fast 机制的。

关于变量的解释在源码注释中都有,最好还是应该看英文注释。
/**
* The hash table data.
/
private transient Entry<K,V>[] table;
/
*
* The total number of entries in the hash table.
/
private transient int count;
/
*
* The table is rehashed when its size exceeds this threshold. (The * value of this field is (int)(capacity * loadFactor).)
*
* @serial
/
private int threshold;
/
*
* The load factor for the hashtable.
*
* @serial
/
private float loadFactor;
/
*
* The number of times this Hashtable has been structurally modified
* Structural modifications are those that change the number of entries in
* the Hashtable or otherwise modify its internal structure (e.g., * rehash). This field is used to make iterators on Collection-views of
* the Hashtable fail-fast. (See ConcurrentModificationException).
*/
private transient int modCount = 0;

构造方法
Hashtable 一共提供了 4 个构造方法:

  • public Hashtable(int initialCapacity, float loadFactor)
    : 用指定初始容量和指定加载因子构造一个新的空哈希表。useAltHashing 为 boolean,其如果为真,则执行另一散列的字符串键,以减少由于弱哈希计算导致的哈希冲突的发生。
  • public Hashtable(int initialCapacity)
    :用指定初始容量和默认的加载因子 (0.75) 构造一个新的空哈希表。
  • public Hashtable()
    :默认构造函数,容量为 11,加载因子为 0.75。
  • public Hashtable(Map<? extends K, ? extends V> t)
    :构造一个与给定的 Map 具有相同映射关系的新哈希表。
/** 
  * Constructs a new, empty hashtable with the specified initial 
  * capacity and the specified load factor. 
  * 
  * @param initialCapacity the initial capacity of the hashtable. 
  * @param loadFactor the load factor of the hashtable. 
  * @exception IllegalArgumentException if the initial capacity is less 
  * than zero, or if the load factor is nonpositive. 
  */ 
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); 
        useAltHashing = sun.misc.VM.isBooted() && (initialCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
 } 
/** 
  * Constructs a new, empty hashtable with the specified initial capacity 
  * and default load factor (0.75). 
  * 
  * @param initialCapacity the initial capacity of the hashtable. 
  * @exception IllegalArgumentException if the initial capacity is less 
  * than zero. 
  */
public Hashtable(int initialCapacity) {  
    this(initialCapacity, 0.75f); 
} 
/** 
  * Constructs a new, empty hashtable with a default initial capacity (11) 
  * and load factor (0.75). 
  */ 
public Hashtable() { this(11, 0.75f); } 
/** 
  * Constructs a new hashtable with the same mappings as the given 
  * Map. The hashtable is created with an initial capacity sufficient to 
  * hold the mappings in the given Map and a default load factor (0.75). 
  * 
  * @param t the map whose mappings are to be placed in this map. 
  * @throws NullPointerException if the specified map is null. 
  * @since 1.2 
  */ 
public Hashtable(Map<? extends K, ? extends V> t) {   
    this(Math.max(2*t.size(), 11), 0.75f); putAll(t); 
}

put 方法
put 方法的整个流程为:
判断 value 是否为空,为空则抛出异常;
计算 key 的 hash 值,并根据 hash 值获得 key 在 table 数组中的位置 index,如果 table[index] 元素不为空,则进行迭代,如果遇到相同的 key,则直接替换,并返回旧 value;
否则,我们可以将其插入到 table[index] 位置。

public synchronized V put(K key, V value) { 
    // Make sure the value is not null确保value不为null 
    if (value == null) { throw new NullPointerException(); } 
    // Makes sure the key is not already in the hashtable.   
    //确保key不在hashtable中 
    //首先,通过hash方法计算key的哈希值,并计算得出index值,确定其在table[]中的位置 
    //其次,迭代index索引位置的链表,如果该位置处的链表存在相同的key,则替换value,返回旧的value 
    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; return old; 
    } } 
    modCount++; 
    if (count >= threshold) { 
        // Rehash the table if the threshold is exceeded 
        //如果超过阀值,就进行rehash操作 rehash(); 
        tab = table; 
        hash = hash(key); 
        index = (hash & 0x7FFFFFFF) % tab.length; 
    } 
    // Creates the new entry. 
    //将值插入,返回的为null 
    Entry<K,V> e = tab[index]; 
    // 创建新的Entry节点,并将新的Entry插入Hashtable的index位置,并设置e为新的Entry的下一个元素   
    tab[index] = new Entry<>(hash, key, value, e);   
    count++; 
    return null; 
}

通过一个实际的例子来演示一下这个过程:
假设我们现在Hashtable的容量为5,已经存在了(5,5),(13,13),(16,16),(17,17),(21,21)这 5 个键值对,目前他们在Hashtable中的位置如下:


图1

现在,我们插入一个新的键值对,put(16,22),假设key=16的索引为1.但现在索引1的位置有两个Entry了,所以程序会对链表进行迭代。迭代的过程中,发现其中有一个Entry的key和我们要插入的键值对的key相同,所以现在会做的工作就是将newValue=22替换oldValue=16,然后返回oldValue=16.


图2

然后我们现在再插入一个,put(33,33),key=33的索引为3,并且在链表中也不存在key=33的Entry,所以将该节点插入链表的第一个位置。
图3

get 方法

相比较于 put 方法,get 方法则简单很多。其过程就是首先通过 hash()方法求得 key 的哈希值,然后根据 hash 值得到 index 索引(上述两步所用的算法与 put 方法都相同)。然后迭代链表,返回匹配的 key 的对应的 value;找不到则返回 null。

public synchronized V get(Object key) { 
    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)) { 
            return e.value; 
        } 
    } 
    return null; 
}

Hashtable 遍历方式
Hashtable 有多种遍历方式:

//1、使用keys()
Enumeration<String> en1 = table.keys(); while(en1.hasMoreElements()) { 
    en1.nextElement();
}
//2、使用elements()
Enumeration<String> en2 = table.elements(); while(en2.hasMoreElements()) { 
    en2.nextElement();
}
//3、使用keySet()
Iterator<String> it1 = table.keySet().iterator(); while(it1.hasNext()) { 
    it1.next();
}
//4、使用entrySet()
Iterator<Entry<String, String>> it2 = table.entrySet().iterator(); 
while(it2.hasNext()) {
    it2.next();
}

Hashtable 与 HashMap 的简单比较

  • HashTable 基于 Dictionary 类,而 HashMap 是基于 AbstractMap。Dictionary 是任何可将键映射到相应值的类的抽象父类,而 AbstractMap 是基于 Map 接口的实现,它以最大限度地减少实现此接口所需的工作。
  • HashMap 的 key 和 value 都允许为 null,而 Hashtable 的 key 和 value 都不允许为 null。HashMap 遇到 key 为 null 的时候,调用 putForNullKey 方法进行处理,而对 value 没有处理;Hashtable遇到 null,直接返回 NullPointerException。
  • Hashtable 方法是同步,而HashMap则不是。我们可以看一下源码,Hashtable 中的几乎所有的 public 的方法都是 synchronized 的,而有些方法也是在内部通过 synchronized 代码块来实现。所以有人一般都建议如果是涉及到多线程同步时采用 HashTable,没有涉及就采用 HashMap,但是在 Collections 类中存在一个静态方法:synchronizedMap(),该方法创建了一个线程安全的 Map 对象,并把它作为一个封装的对象来返回。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容

  • 实际上,HashSet 和 HashMap 之间有很多相似之处,对于 HashSet 而言,系统采用 Hash 算...
    曹振华阅读 2,499评论 1 37
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,213评论 0 16
  • 清晨6点,当我像往常一样拿起手机,关闭 “飞行模式” 的那一秒,一条微信跳了进来。 “小达,我好像抑郁了,能和你聊...
    晓达亲子情商疗愈阅读 1,018评论 3 10
  • Markdown语法收集整理 写在前面的话 网上绝对是已经存在不少相关文章了,但是本人并不自觉画蛇添足,因为很多文...
    白於青阅读 2,787评论 0 1
  • 春意正浓 鸟儿声声渐娇脆 且看春已落何处 行色从容觅春归 树叶待绿草已葱 处处皆显春! 携三五好友 勿问春将归何处...
    余梦人生阅读 367评论 2 4