Board logo

标题: Java 容器源码分析之Map-Set-List(7) [打印本页]

作者: look_w    时间: 2018-12-24 17:05     标题: Java 容器源码分析之Map-Set-List(7)

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)的抽象父类,每个键和值都是对象(源码注释为:The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object.)。但在这一点我开始有点怀疑,因为我查看了HashMap以及TreeMap的源码,都没有继承于这个类。不过当我看到注释中的解释也就明白了,其 Dictionary 源码注释是这样的:NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class. 该话指出 Dictionary 这个类过时了,新的实现类应该实现Map接口。
Hashtable 源码解读成员变量Hashtable是通过"拉链法"实现的哈希表。它包括几个重要的成员变量:table, count, threshold, loadFactor, modCount。
关于变量的解释在源码注释中都有,最好还是应该看英文注释。
[url=][/url]
/**     * 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;[url=][/url]




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0