• 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, take ithmap to study (嘻 ~~ is a knife)? Hashmap can be described as a great tool for JDK, map each Object, implement the fast access of the "key-value". What did you do in actually? Before this, first 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, Hashmap is a declared Map, Cloneable, Serializable interface, and inheriting the AbstractMap class, which inside the Iterator is actually its internal class Hashiterator and several other several. The Iterator class is implemented. Of course, there is also 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 internal class of 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, not very deepest, actually Empty, it will return a static object as a key value, which is why HashMap allows the null key value. ??????? Int hash = hash (k); ??????? 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 I have a payment of 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 in 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. genecordAccess (this); empty method, leave the realization ??????????????? Return OldValue; return the same key value The corresponding old value. ???????????} ???????} ??????? MODCOUNT ; structural change times ??????? Addentry (Hash, K, Value, i ); Add new elements, the key! ??????? Return null; there is no other key value to return}? We take the key way to analyze: ??? Void Addentry (int Hash, Object Key, Object Value, int bukeet) {???? ??? Table [BucketIndex] = New Entry (Hash, Key, Value, Table [BUCKETINDEX]); because the Hash's algorithm is likely to have the same HASH code as the different key values and have the same Table index, such as key = "33" and Key = Object G is all -8901334, then it is Item I after INDEXFOR, so that this Entry's next will point to this original table [i], then This is also true, forming a linked list, and the loop of the PUT is set to the old value. Here, have the structure of Hashmap, everyone knows? ???? ??????? IF (size > = threshold) this Threshold is the amount of actual accommodation ??????????? Resize (2 * Table.Length); exceeding this capacity Will reconstruct Object Table, the so-called reconstruction is not God, it is to build a twice large Table (I saw someone in other forums said that it twice plus 1, I cheated it), then another IndexFor Go in! note! ! This is efficiency! ! If you can make your havehmap don't need to refactor so many times, the efficiency will be greatly improved! ???}