Depth analysis and comparison of HashMap in Java

Source: Internet
Author: User
Tags comparison empty hash insert
Comparison in the Java world, regardless of class or data, the structure of the process is the logic of the entire program and the key to performance. Because I contacted a question about the coexistence of performance and logic, I began to study this problem. Searched the forum of big and Small, also "Java Virtual machine Specification", "apress,.java.collections." (2001),. Bm.ocr.6.0.shareconnector ", and" thinking in Java "turned out also can not find a very good answer, so angrily to the JDK src extract the study, expand the cheerful, hence write this article, Sharing feelings with you and by the way verify that I understand there are no loopholes. Here, take HashMap to study it.

HashMap is a major JDK utility, mapping each object to achieve a "key-value" corresponding to fast access. But what does it actually do?

Before that, let's introduce the load factor and the properties of the capacity. We all know that actually a HashMap's actual capacity on the factor * capacity, its default value is 16x0.75=12; This is very important, it has a certain impact on efficiency! When the HashMap object exceeds this capacity, HashMap reconstructs the Access table. That's a big question, I'll go over it later, anyway, if you already know how many objects you're going to store, it's best to set the acceptable number for that actual capacity.

Two key methods, put and get:

First, there is a concept, HashMap is declared map,cloneable, Serializable interface, and inherited the Abstractmap class, inside the iterator is essentially its internal class hashiterator and several other Iterator class implementation, of course, there is a very important inherited map.entry Entry inner class, because everyone has the source code, we are interested to see this part, I mainly want to explain the Entry internal class. It contains the four attributes of Hash,value,key and next, which is important. Put the source code as follows

The public object is put (object key, object value) {
Object k = masknull (key);

This is to determine whether the key value is empty, is not very esoteric, in fact, if it is empty, it will return a static Object as the key value, which is why hashmap allow null key values.

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

This two consecutive steps is the HashMap of the most cattle place! After the study I was ashamed, where the hash is through the key of the object Hashcode hash, and then through the indexfor to get the index value in object table.

Table??? Don't be surprised, in fact HashMap also God not where to go, it is to use a table to put. The best thing is to use hash to return the index correctly. One of the hash algorithm, I and JDK author Doug contacted, he advised me to see "The Art of programing Vol3" Hateful is, I have been looking for, I can not find, he said, I will be more urgent, but empty pockets Ah!!!

I do not know if you have noticed that put is actually a return method, it will be the same key value to cover up and return the old value! The following method thoroughly illustrates the structure of the HASHMAP, which is actually a table plus a list in the corresponding position of the entry list:

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; Assigns the new value to the corresponding key value.
E.recordaccess (this); Empty method, left to implement
return oldvalue; Returns the corresponding old value of the same key value.
}
}
modcount++; Number of structural changes
AddEntry (hash, k, value, I); Add new elements, the key!
return null; No same key value returned
}

We take the critical approach and analyze it:

void AddEntry (int hash, object key, object value, int bucketindex) {
Table[bucketindex] = new Entry (hash, key, value, Table[bucketindex]);

Because the hash algorithm is likely to make different key values have the same hash code and have the same table index, such as: key= "33" and key=object g of the hash is-8901334, then it passed indexfor after the index must be I, So at new time this entry next will point to the original Table[i], and then the next one, form a linked list, and put the loop on the set e.next get the old values. Here, the structure of the hashmap, we also understand it?

if (size++ >= threshold)//This threshold is the amount that can actually be accommodated
Resize (2 * table.length); Exceeding this capacity will refactor the object table

The so-called refactoring is also not God, is to build a twice times the size of the table (I saw in other forums that someone said is twice times plus 1, cheated me), and then a indexfor in! Attention!! This is efficiency!! If you can make your hashmap do not need to refactor so many times, the efficiency will be greatly improved!

It's pretty much the same here, getting better than put, and we don't know put,get. For collections I think, it is suitable for a wide range, when not entirely suitable for specific, if everyone's program needs special use, write it, in fact, very simple. (the author is like this, he also suggested that I use Linkedhashmap, I saw the source code later found that Linkhashmap is actually inherited HashMap, and then override the corresponding method, interested colleagues, their own looklook) to build an Object Table, write the corresponding algorithm, OK.

For example, like vector,list ah what is actually very simple, most of the synchronization of the statement, in fact, if you want to achieve such as Vector, insert, delete a few, you can use an object table to achieve, indexed access, add, and so on.

If you insert, delete more, you can build two object table, then each element with next structure, a table, if you want to insert to I, but I already have elements, with next connected, then size++, and in another table record its position.





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.