Java collection HashMap, HashTable, HashSet detailed

Source: Internet
Author: User
Tags rehash set set

First, set and map relationships

The set represents an unordered collection element, a collection of elements that are not repeatable, and a map that represents a collection of multiple key-value, and the map collection is the extension of the set set, but the name is different, corresponding to the following

Second, the working principle of HashMap

HashMap based onHashing principle, the object is stored and fetched through the put () and get () methods. Put () Method: It calls the Hashcode () method of the Key object to calculate the Hashcode value, and the system determines the position of the element in the bucket based on the hashcode value. If the hashcode return value of the two object key is the same, then their storage location is the same, if the two entry key returns True by Equals, the newly added entry value overrides the entry value in the collection, but the key does not overwrite If these two entry keys return false by Equals, the newly added entry will form a entry chain with entry in the collection, and the newly added entry is located in the head of the entry chain. Put the source code as follows:
 Publicv put (K Paramk, v paramv) {//if key is empty, call the Putfornullkey method        if(Paramk = =NULL)            returnPutfornullkey (PARAMV); //calculates the hash value based on the keycode of key        inti =Hash (Paramk.hashcode ()); //searches for the corresponding index in the table for the specified hash value        intj = indexfor (i, This. Table.length); //If the entry at the J index is not empty, iterate through the next element of the Localentry element         for(Entry localentry = This. Table[j]; Localentry! =NULL; Localentry =localentry.next) {Object localObject1; //find the specified key equal to put key (hash value is the same, return true by equals)            if(Localentry.hash = =i)&& (((LocalObject1 = localentry.key) = = Paramk) | |(paramk. Equals (LocalObject1))))) {Object LocalObject2=Localentry.value; Localentry.value=PARAMV; Localentry.recordaccess ( This); returnLocalObject2; }        }        //if J index entry is null, there is no entry here         This. modcount + = 1; //add key, value to index at Iaddentry (i, Paramk, PARAMV, J); return NULL; }    voidAddEntry (intParamInt1, K Paramk, V PARAMV,intParamInt2) {        //gets the specified Bucketindex index at entryEntry Localentry = This. Table[paramint2]; //Place the newly created entry into the Bucketindex index and let the new entry point to the original entry         This. table[paramint2] =NewEntry (ParamInt1, Paramk, PARAMV, localentry); //if the number of key-value in the map exceeds        if( This. size++ >= This. Threshold)//Table object is extended to twice times the lengthResize (2 * This. Table.length); }

Put method three cases,:

Get () Method: When the entry stored in each bucket of HashMap is only a single entry, that is, when the entry chain is not generated by the pointer, HashMap has the best performance at this time. When the program takes a key out of the corresponding value, the system calculates the hashcode () return value of the key, and then finds the index of the key in the table array according to the Hashcode return value, then takes out the entry at that index and returns the value corresponding to the key. Get source code is as follows:

 PublicV Get (Object paramobject) {//if key is empty, call Getfornullkey to remove the corresponding value        if(Paramobject = =NULL)            returnGetfornullkey (); //hash code is calculated based on the hashcode value of key        inti =Hash (Paramobject.hashcode ()); //directly remove the value at the specified index in the table arrayEntry Localentry = This. table[indexfor (I, This. Table.length)];  while(Localentry! =NULL) {Object localobject; //If the entry key is the same as the key being searched            if(Localentry.hash = =i)&& (((localobject = localentry.key) = = Paramobject) | |(paramobject. Equals (Localobject))))) returnLocalentry.value; //search for the next entry chainLocalentry =Localentry.next; }        return NULL; As seen from the code, there is only one entry,hashmap in each bucket of hashmap that can quickly take out the entry in the bucket based on the index. 
in the case of a hash conflict, a single bucket is not a entry, but a entry chain, and the system can only traverse each entry sequentially until it finds the entry it wants to search.

HashMap has two parameters that affect its performance:

1. Initial capacity and load factor. The default initial capacity is 16, and the load factor is 0.75. Capacity is the number of buckets (entry arrays) 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.

2. Loading factor is too high while reducing the space overhead, it also increases the query cost (the load factor is the degree to which the elements in the Hsah table are filled.) if: The larger the load factor, the more elements filled, the advantage is that space utilization is high, but the chance of conflict increases. Conversely, the smaller the load factor, the less elements The advantage is that the chances of conflict are reduced, but the space is much wasted. 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 (in fact, the maximum number of entries is less than the initial capacity * load factor).

3.HASHMAP storage elements more and more, to reach the threshold (threshold) threshold, it is necessary to expand the entry array, which is the Java Collection Class framework The greatest charm, hashmap in the expansion, the capacity of the new array will be the original twice times, due to the capacity changes, Each element of the original needs to be recalculated bucketindex, and then stored in the new array, known as the rehash. HashMap default initial capacity 16, load factor 0.75, that is, the maximum can be put 16*0.75=12 elements, when put 13th, HashMap will occur rehash,rehash a series of processing compared to affect performance, So when we need to store more elements to HashMap, it is best to specify the appropriate initial capacity and loading factor, otherwise hashmap can only save 12 elements by default, and multiple rehash operations will occur.

Iii. the difference between HashMap and Hashtable

HashMap and Hashtable all implement the map interface, the main differences are: thread safety, Synchronization (synchronization), and speed. HashMap can be almost equivalent to Hashtable, except that HashMap is non-synchronized and can accept null (HashMap can accept null key value (key) and values (value), and Hashtable does not).

HashMap is non-synchronized, and Hashtable is synchronized, meaning Hashtable is thread-safe, multiple threads can share a hashtable, and multiple threads cannot share hashmap. Java 5 provides Concurrenthashmap, which is an alternative to Hashtable that is better than Hashtable extensibility. Another difference is that the HashMap iterator (Iterator) is the Fail-fast iterator, and the Hashtable enumerator iterator.HashMap can be synchronized using the following statement: Map m = Collections.synchronizemap (HASHMAP); iv. The difference between HashMap and HashSetHashSet implements the set interface, which does not allow duplicate values in the set, HashMap implements the map interface, and the map interface maps the key-value pairs. HashSet expands the HashMap, so the underlying still uses the map store, the storage implementation is consistent with the map, HashMap store key values, HashSet storage objects.

Java collection HashMap, HashTable, HashSet detailed

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.