Java hashmap analysis: Basic Structure

Source: Internet
Author: User

Java hashmap is very commonly used. This article studies its implementation algorithm, and finally hopes to calculate the quantitative data of memory usage and performance, and then draws a conclusion on when hashmap is used and when it cannot be abused.
Hashmap is actually an array, and each element in the array is a linked list. When each element is put into a hashmap using the put method, perform the following steps:
1. Calculate the hash value based on the hashcode provided by the element. The hash value is the subscript of the array.
2. Add new elements to the linked list at the position of the array.

Let's take a look at the definition of Arrays:

 

[Java]View plaincopyprint?

 
  1. /**
  2. * The table, resized as necessary. length must always be a power of two.
  3. */
  4. Transient entry [] table;


This is an array. The transient keyword tells us that it will not participate in serialization. Since it is an array, there is always an upper limit, which means that if too many elements are stored in the hashmap, the array size must be adjusted when the array size cannot store all linked lists. First, let's look at the algorithms related to array capacity.
First, what type of entry?

 

 

[Java]View plaincopyprint?

 
  1. Static class entry <K, V> implements map. Entry <K, V> {
  2. Final K key;
  3. V value;
  4. Entry <K, V> next;
  5. Final int hash;
  6. /**
  7. * Creates new entry.
  8. */
  9. Entry (int h, K, V v, entry <K, V> N ){
  10. Value = V;
  11. Next = N;
  12. Key = K;
  13. Hash = h;
  14. }
  15. ....
  16. Public final Boolean equals (Object O ){
  17. If (! (O instanceof map. Entry ))
  18. Return false;
  19. Map. Entry E = (Map. Entry) O;
  20. Object k1 = getkey ();
  21. Object k2 = E. getkey ();
  22. If (k1 = k2 | (K1! = NULL & k1.equals (K2 ))){
  23. Object V1 = getvalue ();
  24. Object v2 = E. getvalue ();
  25. If (V1 = V2 | (V1! = NULL & v1.equals (V2 )))
  26. Return true;
  27. }
  28. Return false;
  29. }
  30. Public final int hashcode (){
  31. Return (Key = NULL? 0: Key. hashcode () ^
  32. (Value = NULL? 0: value. hashcode ());
  33. }
  34. ....


This is an internal static class of the hashmap class. Implemented the map. entry interface. Two template parameters K and V are accepted. Once the key and hash are initialized in the constructor, they cannot be changed. Because of the existence of next, the entry can constitute a one-way linked list.
More importantly, the equals and hashcode methods. The code is listed first and then explained later.

Second, set the initial capacity
Most of them are in the following constructor. The specified initialcapacity cannot be less than 0 or exceed the maximum value. And the final capicity must be the Npower of 2. If you use a non-parameter constructor, an array with 16 elements is created by default.

 

 

[Java]View plaincopyprint?

 
  1. Public hashmap (INT initialcapacity, float loadfactor ){
  2. If (initialcapacity <0)
  3. Throw new illegalargumentexception ("illegal initial capacity:" +
  4. Initialcapacity );
  5. If (initialcapacity> maximum_capacity)
  6. Initialcapacity = maximum_capacity;
  7. If (loadfactor <= 0 | float. isnan (loadfactor ))
  8. Throw new illegalargumentexception ("illegal load factor:" +
  9. Loadfactor );
  10. // Find a power of 2> = initialcapacity
  11. Int capacity = 1;
  12. While (capacity <initialcapacity)
  13. Capacity <= 1;
  14. This. loadfactor = loadfactor;
  15. Threshold = (INT) (capacity * loadfactor );
  16. Table = new entry [capacity];
  17. Init ();
  18. }


Third, when should I adjust the array size?
The algorithm is like this. There is a variable size that saves the number of elements used by the actual array, and if the value of size reaches the value of the variable threshold, the array capacity must be expanded. Threshold = capicity * loadfactor. capicity is the maximum number of elements contained in the array. loadfactor can be specified in the constructor; otherwise, the default value is 0.75f. The maximum value of capicity is 1 <30 (that is, the power of 2 to the power of 30, 1073741824). We can see that hashmap can store a maximum of 1 billion linked lists.
Fourth, how to adjust the array size?
The answer is 2X like the vector allocation policy in C ++.

 

 

[Java]View plaincopyprint?

 
  1. Void addentry (INT hash, K key, V value, int bucketindex ){
  2. Entry <K, V> E = table [bucketindex];
  3. Table [bucketindex] = new entry <K, V> (hash, key, value, e );
  4. If (size ++> = threshold)
  5. Resize (2 * Table. Length );
  6. }


Fifth, Why must the array size be a multiple of 2?
This will be answered later when we introduce the hash algorithm.

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.