關於new HashMap<>(1)中1的理解(hashMap的初始容量)


新入公司,閱讀代碼的時候發現了一行代碼,為

   

Map<String, String> map=new HashMap<>(1);

對於這個括號里面的1不能理解,於是查了資料,大概了解了。

解釋如下:這個1被稱為初始容量。

API:

/**
     * 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);
    }

 

關於 DEFAULT_LOAD_FACTOR 加載因子 在下文有描述

文章后續加入補充:https://www.cnblogs.com/wangzun/p/13087540.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM