Default initial capacity, load factor, expansion increment for ArrayList, Vector, HashMap, HashTable, HashSet

Source: Internet
Author: User

Here are the reasons to discuss these commonly used default initial capacity and expansion:

When the underlying implementation involves scaling up, the container or reassigning a larger contiguous memory (if the discrete allocation does not need to be redistributed, the discrete allocation is the dynamic allocation of memory when inserting new elements), to copy the container's original data all to the new memory, which undoubtedly makes the efficiency greatly reduced.

The factor of the load factor is less than or equal to 1, meaning that when the number of elements exceeds the factor of the capacity length * load factor, the expansion is performed.

In addition, the expansion also has a default multiple, different container expansion situation is different.

List elements are ordered, repeatable

ArrayList, vector Default initial capacity is 10

Vector: Thread safe, but slow

The underlying data structure is the array structure

Load factor is 1: When the number of elements exceeds the capacity length, the expansion

Expansion increment: 1 time times the original capacity

If the capacity of the vector is 10, the capacity is 20 after one expansion

ArrayList: Thread insecure, fast query speed

The underlying data structure is the array structure

Expansion increment: 0.5 times times the original capacity +1

If the capacity of the ArrayList is 10, the capacity is 16 after one expansion

Set Set elements are unordered and non-repeatable.

HashSet: Thread insecure, fast access speed

The underlying implementation is a HASHMAP (save data) that implements the set interface

The default initial capacity is 16 (why 16, see below for a description of HashMap)

Load factor 0.75: Expands when the number of elements exceeds 0.75 times times the length of the capacity

Expansion increment: 1 time times the original capacity

If the capacity of the HashSet is 16, the capacity is 32 after one expansion

Map is a two-column collection

HASHMAP: The default initial capacity is 16, and the length always remains at 2 N

(Why 16:16 is 2^4, can improve query efficiency, in addition, 32=16<<1-For detailed reasons can be analyzed separately, or analyze source code)

Load factor 0.75: Expands when the number of elements exceeds 0.75 times times the length of the capacity

Expansion increment: 1 time times the original capacity

If the capacity of the HashMap is 16, the capacity is 32 after one expansion

HashTable: Default initial capacity is 11

Thread safe, but slow, key/value is not allowed to be null

Load factor 0.75: Expands when the number of elements exceeds 0.75 times times the length of the capacity

Expansion increment: +1 of the original array length

If the capacity of the Hashtable is 11, the capacity is 23 after one expansion

The specific differences between Hashtable and HashMap can be consulted: http://www.cnblogs.com/xiaoming0601/p/5864022.html

Default initial capacity, load factor, expansion increment for ArrayList, Vector, HashMap, HashTable, HashSet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.