Hence, a HashMap with default capacity 16 can contain more than 11/16 objects(K,V) without rehashing
This is an obvious flaw in using the number of buckets occupied as a measure. Another problem is that you would need to maintain both a size and a number of buckets used.
Instead it's the size() which is used so the size() is the only thing which determines when rehashing occurs no matter how it is arranged.
From the source for Java 8
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // pre-size
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t);
}
else if (s > threshold)
resize();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…