HashMap principle, implementation principle of hashmap

Source: Internet
Author: User

HashMap principle, implementation principle of hashmap
1. Data Structure of HashMap

ArrayFeatures: Easy addressing, difficult insertion and deletion; andLinked ListIt is characterized by hard addressing and easy insertion and deletion. So can we combine the two features to make a data structure that is easy to address and easily inserted and deleted? The answer is yes. This is what we want to mention.Hash tableHash Tables have different implementation methods. What I will explain next is the most commonly used method-the zipper method, which can be understood as"Array of linked list",

We can find that the hash table is composed of arrays and linked lists. In an array with a length of 16, each element stores a head node of the linked list. So what rules are these elements stored in the array. It is generally obtained through hash (key) % len, that is, the hash value of the element's key is modeled on the array length. For example, in the hash table above, 12% 16 = 12,108%, 12,140% = 16 = 12. Therefore, 12, 28, 108, and 140 are stored at the position where the array subscript is 12.

HashMap is actually a linear array, so it can be understood that the container for storing data is a linear array. This may make us puzzled. How does a linear array implement key-value pairs to access data? Here, HashMap does some processing.

1. first, a static internal class Entry is implemented in HashMap. Its important attributes include key, value, next, from attribute key, value, we can see that Entry is a basic bean implemented by the HashMap key-value pair. We mentioned above that the basis of HashMap is a linear array, and this array is Entry [], the content in Map is saved in Entry.

2. Access Implementation of HashMap

Why Random Access to Linear Arrays? Here HashMap uses a small algorithm, which is roughly implemented as follows:

1 // storage: 2 int hash = key. hashCode (); // This hashCode method is not detailed here, as long as you understand that the hash of each key is a fixed int value 3 int index = hash % Entry []. length; 4 Entry [index] = value; 5 6 // value: 7 int hash = key. hashCode (); 8 int index = hash % Entry []. length; 9 return Entry [index];

Here, we can easily understand the basic principle of access through key-value pairs in HashMap.

3. Q: Is there any risk of overwriting if the two keys have the same index through hash % Entry []. length?

Here, HashMap uses the concept of chained data structure. As mentioned above, the Entry class has a next attribute to point to the next Entry. For example, if the first key-value pair A comes in, calculate the hash value of its key to obtain index = 0, and record it as Entry [0] =. After a while, another key-Value Pair B will be added. Its index is equal to 0 after calculation. What should I do now? HashMap will do this: B. next = A, Entry [0] = B. If C is introduced and index is equal to 0, C. next = B, Entry [0] = C; in this way, we find that index = 0 actually accesses three key-value pairs A, B, and C, they are linked together through the next attribute. So don't worry.That is to say, the array stores the last inserted element.By now, we should be clear about the implementation of HashMap.

Of course, HashMap also contains some optimization implementations. For example, after Entry [] has a certain length, as the data in the map grows, the chain of the same index will be long, will it affect the performance? Set a factor (also known as a factor) in HashMap. As the size of map increases, Entry [] will extend the length according to certain rules.

3. Solution to hash conflicts

The hashmap solution in Java is the link address method.

4. Implement your own HashMap

MapTest. java

1 package General; 2 import java. util. *; 3 public class MapTest {4 public static void main (String [] args) {5 Map <String, Employee> staff = new HashMap <> (); 6 staff. put ("144-25-5456", new Employee ("Amy Lee"); 7 staff. put ("567-24-2456", new Employee ("Harry Hacker"); 8 staff. put ("157-62-7935", new Employee ("Gary Cooper"); 9 staff. put ("465-62-5537", new Employee ("Francesca Cruz"); 10 11 // print all entries12 System. out. println (staff); 13 14 // remove an entry15 staff. remove ("567-24-2456"); 16 17 // replace an entry18 staff. put ("456-62-5527", new Employee ("Francesca Miller"); 19 20 // look up a value21 System. out. println (staff. get ("157-62-7935"); 22 23 // iterate through all entries24 for (Map. entry <String, Employee> entry: staff. entrySet () {25 String key = entry. getKey (); 26 Employee value = entry. getValue (); 27 System. out. println ("Key =" + key + ", value =" + value); 28} 29} 30} 31 class Employee {32 private String name; 33 public Employee (String n) {34 name = n; 35} 36 public String getName () {37 return name; 38} 39}View Code1 {157-62-7935 = Amy Lee, 2 567-24-2456 = Harry Hacker, 3 144-25-5456 = Gary Cooper, 4 465-62-5537 = Francesca Cruz} 5 Francesca Miller6 Key = 157-62-7935, value = Amy Lee7 Key = 144-25-5456, value = Harry Hacker8 Key = 465-62-5537, value = Gary Cooper9 Key = 456-62-5527, value = Francesca CruzView Code

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.