Overview of the underlying implementation principles of HashMap

Original text https://blog.csdn.net/fedorafrog/article/details/115478407

hashMap structure

Insert image description here

common problem

After understanding the overall architecture of HashMap, we can try to answer the following questions. If we still have doubts about some of the questions, it means that we still need to go deep into the code and read the book thickly.

  • Why is the bucket array length inside HashMap always an integer power of 2?
  • How big is the default bucket array of HashMap?
  • When does HashMap open up the bucket array to occupy memory?
  • When will HashMap be expanded?
  • When is the linked list of elements in the bucket converted to a red-black tree, and when is it converted back to a linked list? Why is it designed this way?
  • Why is red-black tree introduced in Java 8, and what scenario is it used to solve?
  • How does HashMap handle key-value pairs with null keys?

new HashMap()

In JDK 8, when calling new HashMap(), the array heap memory is not allocated, but some parameter verification is done and some constants are initialized.

public Has

Guess you like

Origin blog.csdn.net/qq_44787816/article/details/129033486