JAVA expansion mechanism and the default size of a common set of common sets of Java expansion

The default size of the expansion mechanism and a common set of Java

During the interview backstage development, the collection is the hot topic of the interview, not only to know the difference between the usage of each set, but also know that the expansion of the collection mechanism, we will talk about the next ArrayList and HashMap default size and expansion mechanism today.

// from ArrayList.java JDK 1.7
private static final int DEFAULT_CAPACITY = 10;
 
//from HashMap.java JDK 7
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

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 a new memory,

This is undoubtedly the efficiency is greatly reduced. 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

The 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: 0.5 times the original volume +1

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

 

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

     (Why is 2 ^ 4 is 16:16, query efficiency can be improved, Further, << 1 32 = 16)

     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

During the interview backstage development, the collection is the hot topic of the interview, not only to know the difference between the usage of each set, but also know that the expansion of the collection mechanism, we will talk about the next ArrayList and HashMap default size and expansion mechanism today.

// from ArrayList.java JDK 1.7
private static final int DEFAULT_CAPACITY = 10;
 
//from HashMap.java JDK 7
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

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 a new memory,

This is undoubtedly the efficiency is greatly reduced. 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

The 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: 0.5 times the original volume +1

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

 

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

     (Why is 2 ^ 4 is 16:16, query efficiency can be improved, Further, << 1 32 = 16)

     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

Guess you like

Origin www.cnblogs.com/alex-xyl/p/11298669.html