Java Collection Class initial capacity, load factor, expansion increment

Source: Internet
Author: User

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

Construction Method Summary HashSet ()
HashSet (int initialcapacity)
Constructs a new empty set whose underlying HASHMAP instance has the specified initial capacity and the default load factor (0.75).
HashSet hs=new HashSet (1);
So visible HashSet class, when creating the object is possible to make the size of the capacity, the second in the interim has this function.

Map is a two-column collection

HASHMAP: Default initial capacity is 16

(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 HashSet is 16, the capacity is 32 after one expansion

Java Collection Class initial capacity, load factor, expansion increment

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.