Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
hash algorithm modulo
1. taken hashcode
int hashcode = key.hashCode();这里的返回值可正可负
只要key不变,hash值一定不变
2. ToTadashi
由于取余后,只能是正整数,所以需要对hash值取正
方式一:int absHash = Math.abs(hashcode);
方式二:int result = hashcode&Integer.MAX_VALUE;
经过方式二(效率高),任意数都会变为正
3. Remainder
int num = result%3;
得到一个0、1、2的任意数,此余数和redis的编号匹配