1、典型回答
负载因子 (Load Factor)也叫扩容因子,它是一个用于控制 HashMap 何时进行扩容的参数。当 HashMap 中存储的键值对数量,超过了 HashMap 总容量乘以扩容因子时,HashMap 就会进行扩容操作。
例如 HashMap 的总容量为 16,扩容因子为 0.75,那么当 HashMap 中存储的键值对大于 12 (16*0.75)时HashMap 就会进行扩容。
注:负载因子的值是 0 到 1 之间 (大于 0,小于 1)。
为什么负载因子是 0.75?
对于这个问题,官方给的答案是这样的:
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Highervalues decrease the space overhead but increase the lookup cost reflected in most of the operations of theHashMap class, including get and put). The expected number of entries in the map and its load factor shouldbe taken into account when setting its initial capacity, so as to minimize the number of rehash operations. Ifthe initial capacity is greater than the maximum number of entries divided by the load factor, no rehashoperations will ever occur.