HashMap源码

重要参数

/**
     * 默认的初始容量,必须是2的幂。
     */
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    /**
     * 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换)
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;
    /**
     * 默认装载因子,这个后面会做解释
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
    /**
     * 存储数据的Entry数组,长度是2的幂。看到数组的内容了,接着看数组中存的内容就明白为什么博文开头先复习数据结构了
     */
    transient Entry[] table;
    /**
     * map中保存的键值对的数量
     */
    transient int size;
    /**
     * 需要调整大小的极限值(容量*装载因子)
     */
    int threshold;
    /**
     *装载因子
     */
    final float loadFactor;
    /**
     * map结构被改变的次数
     */
    transient volatile int modCount;
  /**
     * The load factor used when none specified in constructor.
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

初始长度:16

    /**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

构造器

HashMap有4个构造器

  public HashMap();

  public HashMap(int initialCapacity) ;

   public HashMap(int initialCapacity, float loadFactor) ;

   public HashMap(Map<? extends K, ? extends V> m) 

详细说来:

空构造器:初始长度和负载因子使用默认值;

/**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity.
     * @throws IllegalArgumentException if the initial capacity is negative.
     */
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);//(16,0.75f)
    }

自定义长度,自然数,默认因子=0.75

/**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity.
     * @throws IllegalArgumentException if the initial capacity is negative.
     */
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);//(initialCapacity,0.75f)
}

全自定义参数

 /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and load factor.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }

直接放一个map,默认因子0.75

  /**
     * Constructs a new <tt>HashMap</tt> with the same mappings as the
     * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
     * default load factor (0.75) and an initial capacity sufficient to
     * hold the mappings in the specified <tt>Map</tt>.
     *
     * @param   m the map whose mappings are to be placed in this map
     * @throws  NullPointerException if the specified map is null
     */
    public HashMap(Map<? extends K, ? extends V> m) {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    }

获取threshold,容器当期极限值

>>>    :     无符号右移,忽略符号位,空位都以0补齐

     this.threshold = tableSizeFor(initialCapacity);
....
static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= (1 << 30)) ? (1 << 30) : n + 1;
    }
原文地址:https://www.cnblogs.com/chenglc/p/9390364.html