java取模处理
方式一
//对hash值进行取模,将hash值路由到指定的内存队列中,比如内存队列大小8
//用内存队列的数量对hash值取模后,结果一定在0~7之间
//所以任何一个商品的id都会被固定路由到同样的一个内存队列中。
int anInt = (int) (Math.random() * 10);
System.out.println("初始的值:" + anInt);
String key = String.valueOf(anInt);
int h;
int hash = (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
//得到的hash值
System.out.println("得到的hash值:" + hash);
// ListSize 容器的大小
int ListSize = 8;
int index = (ListSize - 1) & hash;
The location to be stored
System.out.println("Location to store:" + index);
方式二
String a = "A";//A可以为任意字符串
int index= Math.abs(a.hashCode()) % 2