Java Collectes --- HashMap Depth Analysis and Comparison

xiaoxiao2021-03-06  70

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, Hashmap is studying (嘻 ~~ is a knife) HashMap can be described as a great tool for JDK, map each Object to the "key-value" corresponding to fast access. What did you do in actually? 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: {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, this is why HashMap allows the empty key value. INT has = hash (k); int i = indexfor (hash, table.length); this continuous two steps is 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 ~~ 5555 I don't know if I have some payable 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; // Nothing to return} We take key ways 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 with different keys and have the same Table index, such as: Key = "33" and key = Object g Hash is -8901334, then It must be i after indexfor, so that this Entry's next will point to this original Table [i] in New, and then the next one, form a linked list, and PUT cyclic control E. NEXT gets an old value. Here, have the structure of Hashmap, everyone knows? IF (Size > = threshold) // This Threshold is the actual volume resize (2 * Table.Length); // Exceeding this capacity will retrieve the Object Table, the so-called refactor is not God, it is to build a two Double Table (I saw someone in other forums said it twice plus 1, deceived me), then another indexfor! note! ! This is efficiency! ! If you can make your havehmap don't need to refactor so many times, the efficiency will be greatly improved! } It is similar to it, GET is much simpler than PUT, everyone, understands PUT, GET is also not much.

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

New Post(0)