Jdk源码解析

Jdk源码解析


HashMap源码部分

1. HashMap 初始化 <源码解析>

	 /**
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
     * (16) and the default load factor (0.75).
     */
     // 使用默认的初始化容量和默认的加载因子0.75 构造一个空的HashMap,其他所有的字段使用默认值。
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }   

​ 其他字段指的是如下这些:

在这里插入图片描述

   /**
     * 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.)
     */
   // 第一次使用的时候 table 会初始化,并根据需要调整大小,
   // 当分配大小时,长度总是2的次幂,
   // 在某些操作中,我们也允许长度为0,为了允许当前不需要的引导机制。
    transient Node<K,V>[] table;

    /**
     * Holds cached entrySet(). Note that AbstractMap fields are used
     * for keySet() and values().
     */
    // 保存缓的entrySet(),注意点 这个AbstractMap字段被用于keySet()和values()。
    // 父类AbstractMap    public abstract Set<Entry<K,V>> entrySet();
    // -####拓展源码##----》当前类下搜索即可 
    transient Set<Map.Entry<K,V>> entrySet;

   /**
     * Returns a {@link Set} view of the mappings contained in this map.
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  If the map is modified
     * while an iteration over the set is in progress (except through
     * the iterator's own <tt>remove</tt> operation, or through the
     * <tt>setValue</tt> operation on a map entry returned by the
     * iterator) the results of the iteration are undefined.  The set
     * supports element removal, which removes the corresponding
     * mapping from the map, via the <tt>Iterator.remove</tt>,
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
     * <tt>clear</tt> operations.  It does not support the
     * <tt>add</tt> or <tt>addAll</tt> operations.
     *  
      // 返回 这个map中包含的一个映射set视图
     * @return a set view of the mappings contained in this map
     */
    public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es;
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
    }

    /**
     * The number of key-value mappings contained in this map.
     */
    // 这个map中包含的key-value的数量
    transient int size;

    /**
     * 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).
     */
     // 这个map在结构上被修改的次数,结构修改是指那些改变hashMap的映射数量或
     // 其他方式修改其内部结构的修改 例如:rehash。这个字段用于使HashMap集合视图上做迭代器操作的快速失败。
     // 参考:ConcurrentModificationException
    transient int modCount;

    /**
     * The next size value at which to resize (capacity * load factor).
     * 用于调整大小的下一个size的值 (容量*负载因子)
     * @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.)
    // 如果数组table没有分配,此字段将保留初始数组容量,或使用零标志·表示 DEFAULT_INITIAL_CAPACITY
    int threshold;

    /**
     * The load factor for the hash table.
     *  hash表的加载因子
     * @serial
     */
    final float loadFactor;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值