• 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;
        }
  • 相关阅读:
    traceroute tracert 路由器地址 清单 192.168.2.1 网关路由器地址
    /usr/local/nginx/sbin/nginx -s reload 失败原因pid 进程记录和当前不符
    Nginx应用-Location路由反向代理及重写策略 请求转发-URL匹配规则 NGINX Reverse Proxy
    (转载)你好,C++(1)C++是什么?C++的“前世今生”
    (原创)cocos2d-x 3.0 示例代码分析5:testBasic
    (原创)cocos2d-x 3.0 示例代码分析4:VisibleRect
    (原创)cocos2d-x 3.0 示例代码分析3:BaseTest
    (摘要)100个伟大的商业理念:理念35:引爆流行
    (摘要)100个伟大的商业理念:理念34:企业社会责任
    (摘要)100个伟大的商业理念:理念32:重新改造
  • 原文地址:https://www.cnblogs.com/chenglc/p/9390364.html
Copyright © 2020-2023  润新知