public class TestUtil {
private static Map<String,Integer> map = new HashMap<>();
public static void main(String[] args){
TestUtil testUtil = new TestUtil();
testUtil.test1();
testUtil.test();
System.out.println(map.size());
}
public void test1() {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int num=100000;
while (num>0){
Random random = new Random();
String hallCode = "";
for(int i=0;i<4;i++) {
int index = random.nextInt(str.length());
hallCode+=str.charAt(index);
}
if(!map.containsKey(hallCode)){
map.put(hallCode,hallCode.hashCode());
num--;
}
}
}
public void test(){
int i=3;
Map<Integer,Integer> result = new HashMap<>();
for(int j=0;j<5;j++) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
int f = entry.getValue() % i;
Integer value = result.get(f);
if (value != null) {
value++;
result.put(f, value);
} else {
result.put(f, 1);
}
}
i=i*2;
System.out.println(result);
result = new HashMap<>();
}
}
}
得出结论,按hash值分配是均衡的