Read the HashMap (1) and javahashmap of Java source code with me.

Source: Internet
Author: User

Read the HashMap (1) and javahashmap of Java source code with me.

Recently, I am very busy. I want to learn and discuss some Java source code and its implemented Data Structure with you,

Not a high level of things. If you are interested, take a look.

1. Why Map? Use HashMap as an example.

In many cases, we need to store data as key-value pairs and obtain values based on keys (values can be simple values or custom objects)

Of course, this can also be achieved using an object array. When searching, you can traverse the array and compare the keywords to obtain the corresponding value.

In terms of performance, traversing large arrays will consume performance.

In terms of API usability, You need to implement your own search logic

Therefore, it is necessary to use HashMap.

2. What is the data structure of HashMap?

I have always been curious about the internal structure of HashMap. After reading the source code, I found that it is implemented using hash, that is, based on hashcode.

The general idea is this:

2.1 first create an array to access data. Suppose we define an Object [] table to access the value of map.

This is easy to understand. Where is the key? I do not want to store keys for the moment

2.2 The hashcode used to obtain the key is converted into an integer by a certain algorithm.

Index. The value range of this index must be 0 = <index <table. length. Then, I use it as the subscript of the array element.

For example, execute the following operation: table [index] = value;

The storage problem is solved.

2.3 How can I obtain this value through the key?

This is too simple. first obtain the hashcode of the key, and then obtain the element subscript index using the same algorithm.

Then value = table [index]

The simple implementation of HashTable is as follows:

Public class SimpleHashMap {private Object [] table; public SimpleHashMap () {table = new Object [10];} public Object get (Object key) {int index = indexFor (hash (key. hashCode (), 10); return table [index];} public void put (Object key, Object value) {int index = indexFor (hash (key. hashCode (), 10); table [index] = value ;} /*** get the corresponding array subscript through hash code and table length ** @ param h * @ param length * @ return */static int indexFor (int h, int length) {return h & (length-1);}/*** calculate a new hash value using a certain algorithm ** @ param h * @ return */static int hash (int h) {h ^ = (h >>> 20) ^ (h >>>> 12); return h ^ (h >>>> 7) ^ (h >>> 4 );} public static void main (String [] args) {SimpleHashMap hashMap = new SimpleHashMap (); hashMap. put ("key", "value"); System. out. println (hashMap. get ("key "));}}

This simple example roughly describes the hashmap implementation process of hash columns.

But it is still immature. I found at least two problems

1. The size of hashmap is fixed.

2. If different keys have the same index obtained through hashcode, this situation exists. How can this problem be solved?

See Article 2

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.