The default size of the collection capacity

Default initial capacity ArrayList, Vector, HashMap, HashTable, HashSet, the load factor, incremental expansion

Here to discuss these common reasons for default initial capacity and expansion is:

When the underlying implementation involves the expansion container or reassign a larger contiguous section of memory (dynamic memory allocation is not required if the distribution is discrete reallocation, inserting a new element is assigned a discrete time), all of the original data replication your container to the new memory, which undoubtedly greatly reduce the efficiency.

Factor load factor is less than or equal to 1, i.e., when the mean length of * the number of elements exceeds the capacity factor when loading coefficient, for expansion.

Further, there is the expansion of the multiple default, different containers expansion situations.

List elements are ordered, repeatable

ArrayList, Vector default initial capacity of 10

** Vector: ** thread-safe, but slower

Underlying data structure is an array of structure

Load factor is 1: That is, when the number of elements exceeds the capacity of a length, for expansion

Incremental expansion: 1 times the original volume

The Vector capacity of 10, after an expansion of a capacity of 20

** ArrayList: ** thread-safe, fast query speed

Underlying data structure is an array of structure

Incremental expansion: 1.5 times the original volume

As ArrayList capacity of 10, after an expansion of a capacity of 15

SET (set) element disordered unrepeatable.

** HashSet: ** thread-safe, fast access speed

The HashMap is an underlying implementation (preservation data), implements the Set interface

The default initial capacity of 16 (16 is why, as described in the bottom of the HashMap)

Load factor is 0.75: i.e., when the number of elements exceeds 0.75 times the length of the capacity, for expansion

Incremental expansion: 1 times the original volume

The HashSet capacity of 16, after an expansion of a capacity of 32

Map is a two-column set

** HashMap: ** default initial capacity of 16, a length of 2 ^ n remain

(Why is 2 ^ 4 is 16:16, query efficiency can be improved, Further, << 1 32 = 16 -> For a detailed analysis of the reason may be separate, or the source code analysis)

Load factor is 0.75: i.e., when the number of elements exceeds 0.75 times the length of the capacity, for expansion

Incremental expansion: 1 times the original volume

The HashMap capacity of 16, after an expansion of a capacity of 32

** HashTable: ** default initial capacity of 11

Thread-safe, but slow and does not allow key / value is null

Load factor is 0.75: i.e., when the number of elements exceeds 0.75 times the length of the capacity, for expansion

Incremental expansion: the original array length + 2 *

As HashTable capacity of 11, after an expansion of a capacity of 23

Released seven original articles · won praise 2 · Views 1184

Guess you like

Origin blog.csdn.net/weixin_41040866/article/details/102962857