博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashMap源码
阅读量:2384 次
发布时间:2019-05-10

本文共 3710 字,大约阅读时间需要 12 分钟。

重要参数

 

/**     * 默认的初始容量,必须是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 HashMap 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 HashMap 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 HashMap 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 HashMap with the same mappings as the     * specified Map.  The HashMap is created with     * default load factor (0.75) and an initial capacity sufficient to     * hold the mappings in the specified Map.     *     * @param   m the map whose mappings are to be placed in this map     * @throws  NullPointerException if the specified map is null     */    public HashMap(Map
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;    }

 

posted @
2018-07-30 14:21 阅读(
...) 评论(
...)

转载地址:http://pwjab.baihongyu.com/

你可能感兴趣的文章
database disk image is malformed解决方法
查看>>
有关error PRJ0003错误的思考
查看>>
实现自定义对话框程序快捷键的两种方法
查看>>
如何对抗微软霸权,google给我们上了一课
查看>>
未能将基于用户的Visual C++项目设置保存到user文件错误的解决
查看>>
获取windows版本信息的做法
查看>>
忆父亲
查看>>
png库结合zlib库使用出现的一个链接问题的解决
查看>>
STL数组和com数组相互转换的做法
查看>>
开发平台软件中关于第三方库管理的一些思考
查看>>
svn创建分支的做法
查看>>
“当前不会命中断点。源代码与原始版本不同”的问题的有效解决办法
查看>>
对面向对象和面向过程的一些新理解
查看>>
软件开发中的资源管理
查看>>
有关博客的一些断想
查看>>
Windows Server2008上安装VS2008出错及解决办法
查看>>
打开word2010每次都要配置进度的解决办法
查看>>
略论并行处理系统的日志设计
查看>>
开发人员应具备的产品设计意识
查看>>
MSComDlg.CommonDialog服务器不能创建对象错误的解决
查看>>