Make writing a habit together! This is the second day of my participation in the “Gold Digging Day New Plan · April More text challenge”. Click here for more details.

background

  1. Why choose this magic number?

process

  • The source code

  • Example demonstration and results Length is 166 13 4 11 2 9 0

    The length is 32:7 14 21 28 3 10 17 24 31 6 13 20 27 29 16 23 30 5 12 19 26 18 15 22 29 4 11 18 25 0

    Length 64: 7 14 21 28 35 42 49 56 63 6 13 20 27 34 41 48 55 62 5 12 19 26 33 40 47 54 61 4 11 18 25 32 39 46 53 60 3 10 17 24 31 38 45 52 59 29 16 23 30 37 44 51 58 1 8 15 22 29 36 43 50 57 0

    Conclusion: length 2 to the N, hashed out, not repeated.

  • Modify the length

The results of:

Conclusion: Hashes have a lot of duplicates.

  • The source of magic numbers

The results of This magic number is exactly the same as a number of length 2 to the N, and the hash will not be repeated.

Why would a designer consider such an implementation? That is, how did he come up with this realization? Or does he have a solid mathematical background and know that this hash is perfect?

summary

  1. HashCode = I * HASH_INCREMENT + HASH_INCREMENT each time a ThreadLocal instance is added to an Entry[], the increment is 0x61C88647.

  2. Element hash position (array subscript) = hashCode & (length-1)

  3. The length is given by source code and cannot be set by the user. The default value is 16, and each expansion is twice the original value. You have to satisfy this expansion in order to satisfy the hash, there’s no duplication.