In Java, the hash code for the String object is s[0]*31n-1 + s[1]*31n-2 + ... + s[n-1] is calculated like this. To illustrate the above equation, s[i] is the ith character in the string, n is the length of the string, and is the index What I'm curious about is why do you use 31 when multiplying? I understand that you have to use a large decimal when multiplying, but why 31 instead of 29, 37, 97?
n-1n-2 is algorithm string java hash
If you look at a book called Effective Java, it explains this part.
" 31 of the formula chosen because it is small. If this value is an even number, the results of the multiplication overflow if the hash value has been lost. Means two is multiplied by the (the drain) bit of movement. Use the minority are some advantages when it is not clear how routinely do so. The good of the 31 can replace the Beat movement and subtraction to multiplication and can improve performance. In other words, 31 * * * * * * * I is like a (I < < 5) - I Of Java, in recent years vm are automatically carries out in this segment optimization. "
Did you explain it?
884 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
597 GDB gets version error when attempting to debug with the Presense SDK (IDE)
606 Uncaught (inpromise) Error on Electron: An object could not be cloned
567 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.