Java Collection HashMap Collection

Source: Internet
Author: User
Tags addall rehash shallow copy concurrentmodificationexception

The implementation of a hash table-based Map interface. This implementation provides all the optional mapping operations and allows null values and null keys to be used. (in addition to not synchronizing and allowing Nulls, theHashMap class is roughly the same as Hashtable .) This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant.

This implementation assumes that the hash function distributes elements correctly between buckets, providing stable performance for basic operations (get and put). The time that is required to iterate the collection view is proportional to the "capacity" (the number of buckets) of the HashMap instance and its size (number of key-value mappings). Therefore, if iteration performance is important, do not set the initial capacity too high (or set the load factor too low).

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor . capacity is the number of buckets in the hash table, and the initial capacity is just the capacity at the time of creation of the Hashtable. A load factor is a scale in which a hash table can reach a full amount before its capacity increases automatically. Doubles the capacity by calling the rehash method when the number of entries in the hash table exceeds the product of the load factor to the current capacity.

Typically, the default load factor (. 75) seeks a tradeoff in time and space costs. The high load factor, while reducing the space overhead, also increases the query cost (which is reflected in the operations of most HashMap classes, including get and put operations). When setting the initial capacity, you should take into account the number of entries required in the mapping and their loading factors in order to minimize the number of rehash operations. The rehash operation does not occur if the initial capacity is greater than the maximum number of entries divided by the load factor.

If many of the mapping relationships are to be stored in the HashMap instance, creating it with a large enough initial capacity will make it more efficient to store the mapping relationship relative to the automatic rehash operation on demand to increase the capacity of the table.

Note that this implementation is not synchronous. If multiple threads access this mapping at the same time, and at least one of the threads modifies the mapping from the fabric, it must remain externally synchronized. (Structural modifications are actions that add or remove one or more mapping relationships; changing only the values associated with the key that the instance already contains are not structural modifications.) This is typically done by synchronizing the objects that naturally encapsulate the mapping. If such an object does not exist, you should use the Collections.synchronizedmap method to "wrap" the map. It is a good idea to do this at creation time to prevent unintended, out-of-sync access to the mappings, as follows:

HashMap

The iterator returned by all of the collection view methods of this class is a quick failure : After the iterator is created, if the mappings are modified from the structure, except through the remove or add method of the iterator itself, Any other change in any way at any time, the iterator will throw concurrentmodificationexception. Therefore, in the face of concurrent modifications, the iterator will soon fail completely, without risking any uncertain behavior at any time in the uncertain future.

Note that the fast failure behavior of iterators cannot be guaranteed and, in general, there is no firm guarantee when there are concurrent changes that are not in sync. The fast-failing iterator does its best to throw concurrentmodificationexception. Therefore, it is wrong to write a method that relies on this exception program: The Fast failure behavior of iterators should only be used to detect program errors.

This class is a member of the Java collections Framework.

Method Details:

The public int size () returns the number of key-value mapping relationships in this map;

public boolean IsEmpty () returns True if this mapping does not contain a key-value mapping relationship;

Public V get (Object key) returns the value mapped by the specified key in this identity hash map, or null if the mapping does not contain any mapping relationships for this key. A return value of NULL does not necessarily mean that the map does not contain any mapping relationships for that key, or that the mapping explicitly maps the key to null. Use the ContainsKey method to differentiate between the two cases.

public boolean ContainsKey (Object key) returns True if this map contains a mapping relationship for the specified key.

Public V put (K key,v value) associates the specified value with the specified key in this mapping. If this mapping previously contained a mapping relationship for that key, the old value is replaced.

public void Putall (map<? extends K,? extends v> m) copies all mappings of the specified mappings to this map, which replaces all mappings that are currently mapped to all the keys for the specified mappings.

Public V Remove (Object key) If there is a mapping of the key in this map, delete it.

public void Clear () Removes all mapping relationships from this mapping.

public boolean Containsvalue (Object value) returns true if the map maps one or more keys to the specified value.

Public Object Clone () returns a shallow copy of this HASHMAP instance: The key and the value itself are not cloned.

Public set<k> KeySet () returns a Set view of the keys contained in this map. The collection is supported by mappings, so changes to the mappings are also reflected in the collection, and vice versa. The collection supports the removal of elements and removes the corresponding mappings from the map through the Iterator.remove, Set.remove, RemoveAll, Retainall, and clear operations. It does not support add or addall operations.

Public collection<v> values () returns a Collection view of the values contained by this map. The collection is supported by mappings, so changes to the mappings are also reflected in the collection, and vice versa. The collection supports the removal of elements and removes the corresponding mappings from the map through the Iterator.remove, Collection.remove, RemoveAll, Retainall, and clear operations. It does not support add or addall operations.

Public set<map.entry<k,v>> EntrySet () returns a collection view of the mappings that this mapping contains. In the returned collection, each element is a map.entry. The collection is supported by mappings, so changes to the mappings are also reflected in the collection, and vice versa. The collection supports the removal of elements and removes the corresponding mappings from the map through the Iterator.remove, Collection.remove, RemoveAll, Retainall, and clear operations. It does not support add or addall operations.

Java Collection HashMap Collection

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.