HashMap Java Doc

Source: Internet
Author: User
Tags rehash concurrentmodificationexception

Original

public class Hashmap<k,v>

Extends abstractmap<k,v>

Implements Map<k,v>, Cloneable, Serializable

1. Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the nul L Key. (The HashMap class is roughly equivalent to Hashtable, except that it's unsynchronized and permits null S.)

2. This class makes no guarantees as to the order of the map; In particular, it does isn't guarantee that the order would remain constant over time.

3. An instance of HashMap have both parameters that affect its performance: initial capacity and load Factor.

The capacity is the number of buckets in the hash table, and the initial capacity are simply the capacity at the time the H Ash table is created. The load factor is a measure of what the hash table is allowed to get before it capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the Hash tab Le isrehashed (that's, internal data structures is rebuilt) so that the hash table has approximately twice the number of Buckets.

As a general rule, the default load Factor (. Offers ) a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of The  hashmap  class, including  get  and  put ). The expected number of entries in the "map and its" load factor should be taken "into" when setting its initial Capaci Ty, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations wil L ever occur.

If Many mappings is to is stored in a  HashMap  instance, creating it with a sufficiently large capac Ity'll allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow th E table. Note that using many keys with the same  hashcode ()  is A sure-to slow-performance of any HA SH table. To ameliorate impact, when keys are  comparable , this class could use comparison order among keys to help B Reak ties.

4. Note that this implementation are not synchronized.  If Multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural Modification is any operation this adds or deletes one or more mappings; merely changing the value associate D with a key, an instance already contains are not a structural modification.) This was typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This was best-done at creation time, to prevent accidental unsynchronized access to the map:

   Map m = collections.synchronizedmap (new HashMap (...));

The iterators returned by any of this class ' s "Collection View Methods" Are fail-fast:if the map is structurally Modified at no time after the iterator are created, in any except through the iterator ' s own  remove   method, the iterator would throw a  concurrentmodificationexception . Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-d Eterministic behavior at a undetermined time in the future.

5. Note the fail-fast behavior of an iterator cannot being guaranteed as it is, generally speaking, impossible NY hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw concurrentmodificationexception on a best-effort basis. Therefore, it would is wrong to the write a program the depended on the this exception for its correctness:the fail-fast Behavio R of Iterators should is used only to detect bugs.

This class is a member of the Java collections Framework.

HashMap Java Doc

Related Article

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.