table
/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;
注释
表,在第一次使用时初始化,并根据需要调整大小。当分配时,长度总是2的幂。(我们还允许在一些操作中允许长度为零,以允许目前不需要的引导机制。)
作用
table是一个数组,在put e元素的时候,会根据e元素的key计算出对应的下标,放入这个数组中,如果在下一个元素e1,根据e1的key计算出的下标,如果这个下标对应的位置已经存放了元素e,会把当前元素e1放到e元素的next属性中(具体可以看Node<K,V>分析),这样就形成了一个链表,故HashMap的存储方式是:数组+链表
entrySet
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* for keySet() and values().
*/
transient Set<Map.Entry<K,V>> entrySet;
size
/**
* The number of key-value mappings contained in this map.
*/
transient int size;
注释
此映射中包含的键-值映射的数目
作用
记录HashMap存储了多少个键值对元素
modCount
/**
* The number of times this HashMap has been structurally modified
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
*/
transient int modCount;
注释
结构修改是指那些改变HashMap中映射数量或修改其内部结构的修改(例如,重新哈希)。此字段用于使HashMap的集合视图上的迭代器快速失效。(见ConcurrentModificationException)。
作用
记录HashMap被修改了多少次
threshold
/**
* The next size value at which to resize (capacity * load factor).
*
* @serial
*/
// (The javadoc description is true upon serialization.
// Additionally, if the table array has not been allocated, this
// field holds the initial array capacity, or zero signifying
// DEFAULT_INITIAL_CAPACITY.)
int threshold;
注释
调整大小的下一个大小值(capacity * load factor)
作用
默认值是12,当数组大小超过这个阈值时,数组进行扩容
loadFactor
/**
* The load factor for the hash table.
*
* @serial
*/
final float loadFactor;
注释
哈希表的装载因子
作用
主要是参与计算table数组的大小,默认值0.75f