Java.util.HashSet

Source: Internet
Author: User

Operations Time complexity Notes
Add, remove, contains, size O (1) Assuming the hash functions has dispersed the elements properly among the buckets
Iteration Proportional to the number of the ELEMEMTS and buckets Do not set the initial capacity too high (or the load factor too low) for the Iteration-critical program
    • Implementing the Java.util.Set interface, internally backed by a JAVA.UTIL.HASHMAP instance (permitting null values).
    • Making no guarantees to the iteration order and the constantness of the order over time either.
    • not synchronized; Must is synchronized externally, for example:set s = collections.synchronizedset (new HashSet ());
    • Iterators returned by the iterator () method is Fail-fast:it would throw a concurrentmodificationexception if the set is M Odified after the creation of the iterator and by any method except through the iterator ' s remove () method. Notice that the fail-fast behavior cannot being guaranteed because nothing can be guaranteed in the presence of unsynchronize D Concurrent modification.

    1. Implements the Java.util.Set interface, internally implemented by the JAVA.UTIL.MAP example (allows null values)
    2. The iteration order is not guaranteed, nor does the iteration order remain constant
    3. Non-synchronous, must be synchronized externally, for example: Set s = collections.synchronizedset (new HashSet ());
    4. The iterator returned by iterator () is fail-fast: If this iterator is created, the set is modified (not by the Remove () method of this iterator), Then the concurrentmodificationexception exception is thrown. (Note that this fail-fast is also not guaranteed, because there is nothing to be guaranteed in asynchronous concurrent modifications)

Java.util.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.