• 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;
        }
  • 相关阅读:
    由吃饺子想到的多线程情况下的数据共享问题
    关于伪静态的几个体会
    最近改造的一款可多选的日历插件,已通过兼容性测试
    对kingthy创作的Vtemplate模板引擎的使用心得
    从前辈们整理的数据库优化经验中得到的一点心得分享
    关于近期对Lucene.Net应用研究学习的总结
    对SharpICTCLAS 1.0的一点小小的修改记录
    转 Blob、DataURL、canvas、image的相互转换
    节日_100
    模板生成_100
  • 原文地址:https://www.cnblogs.com/chenglc/p/9390364.html
Copyright © 2020-2023  润新知