java中三个for循环嵌套查询,每个集合的size()大小不同?取值做判断,会产生许多冗余数据?如何避免?timeList.size()是66,callBarsList.size()是22,currentList.size()是20;分别取值做判断,会循环好多遍,如何能
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/20 18:22:29
![java中三个for循环嵌套查询,每个集合的size()大小不同?取值做判断,会产生许多冗余数据?如何避免?timeList.size()是66,callBarsList.size()是22,currentList.size()是20;分别取值做判断,会循环好多遍,如何能](/uploads/image/z/13311026-26-6.jpg?t=java%E4%B8%AD%E4%B8%89%E4%B8%AAfor%E5%BE%AA%E7%8E%AF%E5%B5%8C%E5%A5%97%E6%9F%A5%E8%AF%A2%2C%E6%AF%8F%E4%B8%AA%E9%9B%86%E5%90%88%E7%9A%84size%28%29%E5%A4%A7%E5%B0%8F%E4%B8%8D%E5%90%8C%3F%E5%8F%96%E5%80%BC%E5%81%9A%E5%88%A4%E6%96%AD%2C%E4%BC%9A%E4%BA%A7%E7%94%9F%E8%AE%B8%E5%A4%9A%E5%86%97%E4%BD%99%E6%95%B0%E6%8D%AE%3F%E5%A6%82%E4%BD%95%E9%81%BF%E5%85%8D%3FtimeList.size%28%29%E6%98%AF66%2CcallBarsList.size%28%29%E6%98%AF22%2CcurrentList.size%28%29%E6%98%AF20%EF%BC%9B%E5%88%86%E5%88%AB%E5%8F%96%E5%80%BC%E5%81%9A%E5%88%A4%E6%96%AD%2C%E4%BC%9A%E5%BE%AA%E7%8E%AF%E5%A5%BD%E5%A4%9A%E9%81%8D%2C%E5%A6%82%E4%BD%95%E8%83%BD)
java中三个for循环嵌套查询,每个集合的size()大小不同?取值做判断,会产生许多冗余数据?如何避免?timeList.size()是66,callBarsList.size()是22,currentList.size()是20;分别取值做判断,会循环好多遍,如何能
java中三个for循环嵌套查询,每个集合的size()大小不同?取值做判断,会产生许多冗余数据?如何避免?
timeList.size()是66,callBarsList.size()是22,currentList.size()是20;
分别取值做判断,会循环好多遍,如何能够控制循环的次数,只让满足条件的数据循环一遍?
for (int j = 0; j < timeList.size(); j++) {
for (int k = 0; k < callBarsList.size(); k++) {
for (int k2 = 0; k2 < currentList.size(); k2++) {
int timeWeekMonth = timeList.get(j).getWeekendingmonth();
int timeWeekDay = timeList.get(j).getWeekendingday();
int current = currentList.get(k2).getCurrentID();
if (callBarsList.get(k).getWeek() == timeList.get(j).getWeek()
&& callBarsList.get(k).getYear().intValue() == timeList.get(j).getYear().intValue()
&& current == callBarsList.get(k).getId()) {
Integer count = callBarsList.get(k).getCount();
String description = callBarsList.get(k).getDescription();
String theDate = timeWeekMonth + "/" + timeWeekDay;
dataset.setValue(count,description,theDate);
}else{
String description=callBarsList.get(k).getDescription();
String theDate= timeWeekMonth+"/"+timeWeekDay;
dataset.setValue(0,description,theDate);
}
}
}
}
java中三个for循环嵌套查询,每个集合的size()大小不同?取值做判断,会产生许多冗余数据?如何避免?timeList.size()是66,callBarsList.size()是22,currentList.size()是20;分别取值做判断,会循环好多遍,如何能
这个三重循环肯定会降低性能.循环次数是66*22*20次.另外计算集合的大小应该放到循环外边声明,避免每次循环都重新计算其大小.如果你用的jdk版本是5.0以上,建议你用for-each循环结构.
改良的代码如下:循环次数是66+22+20
Map callBarMap = new HashMap();
int callBarsize = callBarsList.size();
for (int k = 0; k < size; k++) {
map.put( callBarsList.get(k).getWeek() +","+callBarsList.get(k).getYear().intValue(),callBarsList.get(k));
}
Map currentMap = new HashMap();
int currentSize = currentList.size();
for (int k2 = 0; k2 < currentSize; k2++){
currentMap.put(currentList.get(k2).getCurrentID(),currentList.get(k2));
}
int timeSize = timeList.size();
for (int j = 0; j < timeSize; j++) {
int timeWeekMonth = timeList.get(j).getWeekendingmonth();
int timeWeekDay = timeList.get(j).getWeekendingday();
CallBars callBars =map.get(timeList.get(j).getWeek()+","+timeList.get(j).getYear().intValue);
if(callBars !=null){
Current current = currentMap.get(callBars.getId());
if(current!=null){
Integer count = callBars.getCount();
String description = callBars.getDescription();
String theDate = timeWeekMonth + "/" + timeWeekDay;
dataset.setValue(count,description,theDate);
}else{
String description=callBars.getDescription(); String theDate= timeWeekMonth+"/"+timeWeekDay;
dataset.setValue(0,description,theDate);
}
}
}
}