需求如下:
一个装有若干worker对象的List如
workerList [worker1,worker2,worker3,worker4,worker5,workeer6]
将所有的worker安排工作时间表如:
周一:worker1,worker2,worker3,worker4 workerListMon
周二: worker1,worker3,worker4,worker5 workerListTues
周三: worker1,worker2,worker4,worker5 workerListWed
周四: worker3,worker4,worker5,worker6 workerListThurs
周五:worker1,worker2,worker3,worker6 workerListFri
想将worker按照工作天数由多到少进行排序,并且能够得出工作天数最长的worker的工作日。
个人想法是通过遍历workerList中的每个worker,再到每个工作日list中验证元素是否存在,若存在则计数加1,不知这种方式执行效率是否有问题,而且如何确定workder的工作日是哪几天?
在你的那个方法基础上,如果workerListMon这一类的list遍历完某个数据匹配成功后,可以在list中删除,提高运算速度。
另外如果本身就是list的话,那没有其他的办法,如果当初创建的时候可以自己选择的话,建议使用map,map肯定会比list高效的(当然方法需要稍微的改动下)
class DayWorker{
private Integer workerMun;
private String[] day ;
public Integer getWorkerMun() {
return workerMun;
}
public void setWorkerMun(Integer workerMun) {
this.workerMun = workerMun;
}
public String[] getDay() {
return day;
}
public void setDay(String[] day) {
this.day = day;
}
}
public class Test {
public static Map< String, DayWorker> addMap(Map< String, DayWorker> map, List<String> workerList , int day){
String [] dayStr ={"workerListMon" ,"workerListTues","workerListWed","workerListThurs","workerListFri"} ;
for (String key : workerList) {
if(!map.containsKey(key)){
DayWorker worker = new DayWorker() ;
worker.setWorkerMun(1);
String [] dayName = new String[5] ;
dayName[0] =dayStr[day-1] ;
worker.setDay(dayName) ;
map.put(key, worker) ;
}else{
DayWorker worker = map.get(key) ;
worker.setWorkerMun(worker.getWorkerMun()+1) ;
String[] dayName = worker.getDay() ;
dayName[day-1] = dayStr[day-1] ;
worker.setDay(dayName) ;
}
}
return map ;
}
public static void main(String[] args) throws Exception {
//初始数据
List<String> workerListMon = new ArrayList<String>();
List<String> workerListTues = new ArrayList<String>();
List<String> workerListWed = new ArrayList<String>();
List<String> workerListThurs = new ArrayList<String>();
List<String> workerListFri = new ArrayList<String>();
for (int i = 1; i < 7; i++) {
String s = "worker"+ i ;
if(i == 1 i == 2