Article statement, this article is completely original, as in vain, if there is a mistake, please refer to you! Thank you!
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, it also puts the "Java Virtual Machine Specification", "Apress, .java.collections (2001), BM.Ocr.6.0.Shareconnector, and" Think 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 is to study HashMap (嘻 ~~ is a knife)
Hashmap can be described as a large tool for JDK, map each Object, and realize the quick access of the "key-value". What did you do in actually?
Before this, let's introduce the attributes of load factors 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, the efficiency is very impact! 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, havehmap is a statement that the Map, Cloneable, Serializable interface, and inherits the AbstractMap class, which is actually mainly its internal class hashiterator and several other Iterator classes. Of course there is also an important inheritance. Map.Entry's entry internal class, because everyone has source code, everyone is interested in seeing this part, I mainly want to explain the entry of the entry. It contains Hash, Value, Key and Next
These four attributes are important. The source code of the PUT is as follows
Public Object Put (Object Key, Object Value) {
Object k = masknull (key); this is whether it is empty, it is not very deepest, 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 step 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, unfortunately pockets are empty ~~ 5555
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; assign the new value to the corresponding key value. E.RecordAccess (this); empty method, lasting Return OldValue; returns the corresponding old value of the same key value. }}} Modcount ; the number of structural changes Addentry (Hash, K, Value, i); add new elements, the key! Return NULL; None of the same key value returns} 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, such as: key = "33" and key = Object G's Hash is -8901334, then it passes The index after IndexFor must be i, so that this entry's next will point to this original Table [i], and then the next one is true, and the PUT is looped. E.Next Old value. Here, have the structure of Hashmap, everyone knows? IF (size > = threshold) This Threshold is the actual amount of Resize (2 * Table.Length); exceeding this capacity will reconstruct Object Table, the so-called refactor is not God, it is to build a big one. TABLE (I saw someone in other forums said that it is twice that add 1, and I cheated me), then I will go in again! 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. I think that I am suitable for the whole, it is suitable for unique, if everyone's procedure requires special purposes, it is actually very simple.