java中三个for循环嵌套查询,每个集合的size()大小不同?取值做判断,会产生许多冗余数据?如何避免?
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/09/29 19:26:37
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);
}
}
}
}
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);
}
}
}
}
这个三重循环肯定会降低性能.循环次数是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);
}
}
}
}
再问: 太感激你了,但这样只是计算出了符合条件的数据,那么,我那个if判断符合的加入dataset结果集,不符合的没有执行啊?应该怎么改动,让它执行,如我上面代码的if,else执行条件?
再答: 继续回答限制字数,粘贴的代码超过限制了。你再重新提问一个,我把代码粘给你。
改良的代码如下:循环次数是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);
}
}
}
}
再问: 太感激你了,但这样只是计算出了符合条件的数据,那么,我那个if判断符合的加入dataset结果集,不符合的没有执行啊?应该怎么改动,让它执行,如我上面代码的if,else执行条件?
再答: 继续回答限制字数,粘贴的代码超过限制了。你再重新提问一个,我把代码粘给你。
vb题目在嵌套循环的for循环中如何计算循环次数
java 获取两个map集合中不同的数据,
java标签,用c:forEach循环,将集合里面的数据显示在表格中
用Java编写一个嵌套的for循环打印下列图案:
c语言中 for循环嵌套的运算流程?
JSTL 如何判断集合size
关于matlab的for循环嵌套问题
在java中怎么判断集合里面每个元素的个数
C语言中while(1)中嵌套一个for循环,那么for循环会被一直反复执行吗?
如何将matlab中for循环嵌套去掉,这个能不能去掉
对于for循环中嵌套了if结构的流程图怎么画呢?
用java写一个嵌套的for循环打印下列图案:1 1 2 1 2 3 1 2 3 4 1 2 3 4 5