为了提高数据处理的速度,会经常用到分片的方法。为了数据能够分片均匀分片数该怎么取?
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
for (int i = 0; i <100000000; i++) {
int code = String.valueOf(i).hashCode();
int mode = Math.abs(code%7);
switch(mode){
case 0:
Integer count = map.get(“0”);
if(count == null){
map.put(“0”,1);
}else {
map.put(“0”,++count);
}
continue;
case 1:
Integer count1 = map.get(“1”);
if(count1 == null){
map.put(“1”,1);
}else {
map.put(“1”,++count1);
}
continue;
case 2:
Integer count2 = map.get(“2”);
if(count2 == null){
map.put(“2”,1);
}else {
map.put(“2”,++count2);
}
continue;
case 3:
Integer count3 = map.get(“3”);
if(count3 == null){
map.put(“3”,1);
}else {
map.put(“3”,++count3);
}
continue;
case 4:
Integer count4 = map.get(“4”);
if(count4 == null){
map.put(“4”,1);
}else {
map.put(“4”,++count4);
}
continue;
case 5:
Integer count5 = map.get(“1”);
if(count5 == null){
map.put(“5”,1);
}else {
map.put(“5”,++count5);
}
continue;
case 6:
Integer count6 = map.get(“6”);
if(count6 == null){
map.put(“6”,1);
}else {
map.put(“6”,++count6);
}
continue;
}
}
System.out.println("map = " + map);
consle:当分片数为7时map = {0=14285714, 1=14285717, 2=14285716, 3=14285711, 4=14285714, 5=14285717, 6=14285713};
当分片数为8时map = {0=12500038, 1=12499999, 2=12499964, 3=12499955, 4=12499970, 5=12500000, 6=12500028}
实测看奇偶数对数据分片的影响不大。