Depth Analysis and Comparison of HashMap in Java

xiaoxiao2021-03-06  38

In the world of Java, regardless of the class or various data, the structure of its structure is the key to the logic and performance of the entire program. Since I have contacted a problem with the performance and logic coexisting, I started researching this problem. Looking for a large and small forum, I also find the "Java Virtual Machine Specification", "Apress, .java.collections. (2001), BM.OCR.6.0.SHARECONNECTOR", and "Thinking in Java" Less than a good answer, so I decompressed the JDK's SRC, expanded to open this article, sharing this article, and confirmed that I have no vulnerabilities. Here, take it to study it.

Hashmap can be described as a large tool for JDK, map each Object, and realize the quick access of the "key-value". But what do you do in reality? Before this, you will introduce the properties of the load factor and capacity. Everyone knows that there is actually a HashMap actual capacity to factor * capacity, its default value is 16 × 0.75 = 12; this is very important, it is very impact on efficiency! When the object stored in HashMap exceeds this capacity, HashMap will restructive access tables. This is a big problem, I will slowly introduce it, anyway, if you already know how many objects you want to store, it is best to be able to accept the actual capacity.

Two key methods, PUT and GET: There is such a concept first, and havehmap is declared Map, Cloneable, Serializable interface, and inheriting the AbstractMap class, which is actually mainly its internal class Hashiterator and several other Iterator classes. Realization, of course, there is a very important inheritance of the Entry internal class of Map.Entry, because everyone has source code, everyone is interested in seeing this part, I mainly want to explain the entry of the entry. It contains four properties of Hash, Value, Key, and Next. The source code of the PUT is as follows

Public Object Put (Object Key, Object Value) {Object K = Masknull (Key);

This is the judgment key value is empty, it is not very profound. In fact, if it is empty, it will return a Static Object as a key value, which is why HashMap allows the empty key value.

INT has = hash (k); int i = indexfor (hash, table.length);

This continuous two steps are the most cattle of Hashmap! I have finished sharing, where Hash is HashCode, which is Object Table, is obtained by indexfor's indexFor. TABLE? ? ? Don't be surprised, in fact, Hashmap is not there, it is put it with table. The most cattle is to return indexes correctly with HASH. The Hash algorithm, I contacted the author Doug of JDK, he suggested that "The Art of Programing Vol3" can hate, I have been looking for it before, I can't find it, he like this, I will More anxious, but unfortunately the pocket is empty! ! ! I don't know if you pay attention to the PUT is actually a return method, which will cover the PUT of the same key value and return to the old value! The following method thoroughly illustrates the structure of HashMap, in fact, a list of entry on the corresponding position: for (entry E = Table [i]; E! = Null; E = E.NEXT) {IF (E.hash == Hash && EQ (k, e.key)) {Object OldValue = E.Value; E.Value = value; // Give the new value to the corresponding key value. E.RecordAccess (this); // null method, the rest of the RETURN OLDVALUE; // returns the corresponding old value of the same key value. }}} Modcount ; // Structural changes Adderry (Hash, K, Value, I); // Add new elements, the key! Return null; / / no key value return}

We take the key method to analyze:

Void Addentry (int 6, int bukeet) {table [bucketindex] = new entry (hash, key, value, table [bucketindex]);

Because Hash's algorithm is likely to have the same HASH code as the different key values, such as: key = "33" and key = Object G are all -8901334, which is the index after IndexFor It's all I, so that this entry's next will point to this original Table [i], and then the next one is the same, and the PUT is looped to obtain the old value. Here, have the structure of Hashmap, everyone knows?

IF (size > = threshold) // This Threshold is the actually accommodated amount resize (2 * Table.Length); // Experience the Object Table Reconstruction

转载请注明原文地址:https://www.9cbs.com/read-119382.html

New Post(0)