Introduction to the basic implementation of Java common set framework and the attention point

Source: Internet
Author: User

Collection:

ArrayList:
1: The underlying implementation is an array, the default length is 10.
2:add (), determines whether the array is out of bounds, is the array expansion of twice times the original.
3:remove (), copy array, size-1, frees up empty space.

HashSet:
The underlying implementation is a HASHMAP, the value exists on the key of the map, value is stored in an object, that is, the underlying HASHMAP value is the same.
HashSet are unordered and do not allow repetition.

MAP:


HASHMAP:
Initial capacity value: 10, Load factor 0.75f.
When put (), the first object is initialized with a capacity of 12.
The underlying is a entry array, which, according to the hash value, determines the position of the entry array where the object Val objects exist.
Note that when traversing a map with iterator, the Remove () method cannot be called during traversal, otherwise an error will be made.
Java.util.ConcurrentModificationException
At Java.util.hashmap$hashiterator.nextentry (hashmap.java:922)
At Java.util.hashmap$entryiterator.next (hashmap.java:962)
At Java.util.hashmap$entryiterator.next (hashmap.java:960)

Reason:
When the HashMap iterator is obtained, Expectedmodcount = Modcount;modcount is put in the constructor: (Record map Add, delete, modcount+1)
In the iterative process how to call the Remove () or add () method, Modcont+1, in Iterator.next () will be to compare expectedmodcount and Modcont is equal, unequal, error.

Source:

Introduction to the basic implementation of Java common set framework and the attention point

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.