一、简单的平均算法
/*
* 平均分配
*/
public Map<String,List> allotOfAverage(List users,List tasks){
Map<String,List> allot=new ConcurrentHashMap<String,List>(); //保存分配的信息 ,同步的MAP安全
if(users!=null&&users.size()>0&&tasks!=null&&tasks.size()>0){ //如果任务数大于员工数
for(int i=0;i<tasks.size();i++){
int j=i%users.size(); //任务数下标除员工数取余,保证每个人都有
if(allot.containsKey(users.get(j))){
List list=allot.get(users.get(j));
list.add(tasks.get(i));
allot.put(users.get(j), list);
}else{
List list=new ArrayList();
list.add(tasks.get(i));
allot.put(users.get(j), list);
}
}
}
return allot;
}
二、随机分配算法
/*
* 随机分配
*/
public Map<String,List> allotOfRandom(List users,List tasks){
Map<String,List> allot=new ConcurrentHashMap<String,List>(); //保存分配的信息
if(users!=null&&users.size()>0&&tasks!=null&&tasks.size()>0){
for(int i=0;i<tasks.size();i++){
int r_user=new Random().nextInt(users.size()); //生成随机数下标
if(allot.containsKey(users.get(r_user))){
List list=allot.get(users.get(r_user));
list.add(tasks.get(i));
allot.put(users.get(r_user), list);
}else{
List list=new ArrayList();
list.add(tasks.get(i));
allot.put(users.get(r_user), list);
}
}
}
return allot;
}
三、权重分配
/*
* 权重分配
*/
public Map<String,List> allotOfProportion(Map<String,String> users,List tasks){
Map<String,List> allot=new ConcurrentHashMap<String,List>(); //保存分配的信息
if(users!=null&&users.size()>0&&tasks!=null&&tasks.size()>0){
int a=0;//总权重
for(Entry<String, String> entry:users.entrySet()){
a+=Integer.parseInt(entry.getValue());
}
int start=0,end=0;//起始下标 ,结束下标
if(a>0){
for(Entry<String, String> entry:users.entrySet()){
List allotTask=new ArrayList();
end+=Integer.parseInt(entry.getValue());//权重累计
for(;start < tasks.size()*end/a;start++){
allotTask.add(tasks.get(start));
}
allot.put(entry.getKey(),allotTask);
}
}
}
return allot;
}