Java (3) ----- Map summary, java ----- map summary

Source: Internet
Author: User
Tags rehash

Java (3) ----- Map summary, java ----- map summary

Previously, LZ introduced in detail the implementation methods of HashMap, HashTable, and TreeMap, and elaborated on three aspects: data structure, implementation principle, and source code analysis, we should have a clear understanding of the three classes. The LZ below makes a simple summary of Map.

Recommended reading:

Java improvements (II and III) -- HashMap

Java improvements (Part 2 and Part 5) -- HashTable

Java (ii) ----- hashCode

Java improvements () -- TreeMap

1. Map Overview

First, let's look at the structure of Map.

Int hashValue = Maths. abs (obj. hashCode () % size;

The following are HashMap and HashTable:
---------- HashMap ------------ // calculate the hash value static int hash (int h) {h ^ = (h >>> 20) ^ (h >>> 12 ); return h ^ (h >>> 7) ^ (h >>> 4);} // calculate the index location of the key static int indexFor (int h, int length) {return h & (length-1) ;}----- HashTable -------------- int index = (hash & 0x7FFFFFFF) % tab. length; // confirm the index location of the key
The Location index represents the location of the node in the array. Is the basic principle of hash ing:
In this figure, steps 1-4 are to locate the element in the array, and steps 5-8 are to insert the element into the array. There will be a small setback in the Insert Process. The hash values of multiple elements are the same, so that the same index location is obtained. That is to say, multiple elements are mapped to the same location, this process is called "conflict ". The solution to the conflict is to insert a link list at the index location and simply add the elements to the link list. Of course, it is not a simple insert. The process in HashMap is as follows: Get the linked list at the index position. If the linked list is null, insert the element directly, otherwise, compare whether there are keys with the same key. If so, overwrite the original key value and return the old value, otherwise, the element is saved in the chain header (the first saved element is placed at the end of the chain ). The following is the put Method of HashMap. This method shows in detail the whole process of calculating the index location and inserting elements into the appropriate location:
Public V put (K key, V value) {// when the key is null, call the putForNullKey method to save null and the first position of the table, this is the reason why HashMap allows null. if (key = null) return putForNullKey (value); // calculate the hash value of the key. int hash = hash (key. hashCode (); // calculate the position of the key hash value in the table array. int I = indexFor (hash, table. length); // iterator e from the beginning of I to determine whether the same key for (Entry <K, V> e = table [I]; e! = Null; e = e. next) {Object k; // determines whether the chain has the same hash value (the same key). // if the chain has the same hash value, the value is overwritten, returns the old value if (e. hash = hash & (k = e. key) = key | key. equals (k) {V oldValue = e. value; // old value = new value e. value = value; e. recordAccess (this); return oldValue; // return the old value} // increase the number of modifications by 1 modCount ++; // Add the key and value to the addEntry (hash, key, value, I); return null ;}
The put Method of HashMap shows the basic idea of hash ing. In fact, if we look at other maps, we find that their principles are similar!
Iii. Map Optimization

First, let's assume that the size of the internal array mapped by hash is only 1, and all elements are mapped to this position (0) to form a long linked list. Since we need to perform linear search on this linked list for updates and accesses, this will inevitably reduce the efficiency. We assume that if there is a very large array and each linked list has only one element, the object will be obtained by calculating its index value during access, although this will improve the search efficiency, it will waste the control. Although both methods are extreme, they provide us with an optimization idea:Use a large array to evenly distribute elements.Two Map operations affect the efficiency. One is the container initialization size and the other is the load factor.

3.1 adjust the implementation size

In a hash ing table, each location in the internal array is called a bucket, and the number of available buckets (that is, the size of the internal array) is called capacity ), to enable the Map object to effectively process any number of elements, we design the Map to adjust its own size. We know that when the number of elements in the Map reaches a certain level, the container's size will be adjusted, but the overhead of this resizing process is very large. To adjust the size, you need to insert all the original elements into the new array. We know that index = hash (key) % length. This may cause keys that conflict with each other not to conflict with each other. The overhead of re-calculation, adjustment, and insertion is very large and inefficient. So,If we start to know the expected Map size value and adjust the Map size sufficiently large, it can be greatly reduced or even unnecessary to re-adjust the size, which may increase the speed.The following is the process of adjusting the container size in HashMap. The following code shows the complexity of the expansion process:

Void resize (int newCapacity) {Entry [] oldTable = table; // original container int oldCapacity = oldTable. length; // original container size if (oldCapacity = MAXIMUM_CAPACITY) {// whether it exceeds the maximum value: 1073741824 threshold = Integer. MAX_VALUE; return;} // new array: The size is oldCapacity * 2 Entry [] newTable = new Entry [newCapacity]; transfer (newTable, initHashSeedAsNeeded (newCapacity )); table = newTable;/** recalculate Threshold = newCapacity * loadFactor> MAXIMUM _ CAPACITY + 1? * NewCapacity * loadFactor: MAXIMUM_CAPACITY + 1 */threshold = (int) Math. min (newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);} // Insert the element into the new array void transfer (Entry [] newTable, boolean rehash) {int newCapacity = newTable. length; for (Entry <K, V> e: table) {while (null! = E) {Entry <K, V> next = e. next; if (rehash) {e. hash = null = e. key? 0: hash (e. key);} int I = indexFor (e. hash, newCapacity); e. next = newTable [I]; newTable [I] = e; e = next ;}}}
3.2 load factor

To determine when the Map container needs to be adjusted, Map uses an additional parameter and roughly calculates the density of the storage container. Before Map adjustment, use the "load factor" to indicate the "load volume" that Map will assume, that is, its load level, when the number of elements in the container reaches this "LOAD", Map will resize. The relationship among load factor, capacity, and Map size is as follows: load factor * capacity> map size -----> adjust the Map size.

For example, if the load factor is 0.75 (the default value of HashMap) and the default capacity is 11, then 11*0.75 = 8.25 = 8. So when we insert the eighth element in our container, map will adjust the size.

The load factor itself is a compromise between the control and time. When I use a small load factor, although the possibility of conflict is reduced, the length of a single linked list is reduced, and the access and update speed are accelerated, however, it occupies more controls, making most of the controls in the array unavailable, and the element distribution is sparse. At the same time, due to frequent adjustment of Map size, performance may be reduced. However, if the load factor is too large, the element distribution will be relatively compact, resulting in increased possibility of conflicts, resulting in slow access and update. Therefore, we generally recommend that you do not change the value of the load factor. The default value is 0.75.

Last

Recommended reading:

Java improvements (II and III) -- HashMap

Java improvements (Part 2 and Part 5) -- HashTable

Java (ii) ----- hashCode

Java improvements () -- TreeMap

----- Original from: http://cmsblogs.com /? P = 1212Please respect the author's hard work and repost the source.

----- Personal site:Http://cmsblogs.com


3rd, What plug-ins are used for your small map?

What small map...
 

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.