HashMap之属性

20 篇文章 0 订阅

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值