zoukankan      html  css  js  c++  java
  • HashMap 构造函数

    HashMap总共提供了三个构造函数

    1  /**
    2      * Constructs an empty <tt>HashMap</tt> with the default initial capacity
    3      * (16) and the default load factor (0.75).
    4      */
    5     public HashMap() {
    6         this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    7     }

    解读:这是无参构造函数,构造了一个初始容量16和负载因子为0.75的HashMap实例。
          负载因子介绍请参考本人博客:"HashMap负载因子"

     1 /**
     2      * Constructs an empty <tt>HashMap</tt> with the specified initial
     3      * capacity and the default load factor (0.75).
     4      *
     5      * @param  initialCapacity the initial capacity.
     6      * @throws IllegalArgumentException if the initial capacity is negative.
     7      */
     8     public HashMap(int initialCapacity) {
     9         this(initialCapacity, DEFAULT_LOAD_FACTOR);
    10     }

    解读:构造一个指定初始容量的HashMap实例,此时负载因子还是0.75.

     1     /**
     2      * Constructs an empty <tt>HashMap</tt> with the specified initial
     3      * capacity and load factor.
     4      *
     5      * @param  initialCapacity the initial capacity
     6      * @param  loadFactor      the load factor
     7      * @throws IllegalArgumentException if the initial capacity is negative
     8      *         or the load factor is nonpositive
     9      */
    10     public HashMap(int initialCapacity, float loadFactor) {
    11         if (initialCapacity < 0)
    12             throw new IllegalArgumentException("Illegal initial capacity: " +
    13                                                initialCapacity);
    14         if (initialCapacity > MAXIMUM_CAPACITY)
    15             initialCapacity = MAXIMUM_CAPACITY;
    16         if (loadFactor <= 0 || Float.isNaN(loadFactor))
    17             throw new IllegalArgumentException("Illegal load factor: " +
    18                                                loadFactor);
    19         this.loadFactor = loadFactor;
    20         this.threshold = tableSizeFor(initialCapacity);
    21     }

    解读:指定初始容量和负载因子构造一个HashMap实例.

  • 相关阅读:
    uart协议--Verilog及仿真
    DC综合的流程
    modelsim将vcd文件转换成wlf文件并查看波形
    将系统盘还原成原来的普通u盘
    Ubuntu的操作命令
    gvim快捷键
    dc_shell环境下TCL语言的使用
    AHB总线学习
    Ubuntu文件共享
    Linux基础学习 | 目录及文件
  • 原文地址:https://www.cnblogs.com/yesiamhere/p/6647287.html
Copyright © 2011-2022 走看看