for(CityDataVO cityItem: citys){
boolean flag =false;
for(ProvinceDataVO proItem : list){
xxxxxx
flag = true;
break;
}
if(!flag){
ProvinceDataVO province = new ProvinceDataVO();
province.setProvinceId(cityItem.getProvinceId());
province.setProvinceName(cityItem.getProvinceName());
province.setReportNum(cityItem.getCount());
List<CityDataVO> vo = new ArrayList<>();
vo.add(cityItem);
province.setCityData(vo);
list.add(province);
}
}
For example, in the above code, if I do not enter the second level of for loop, I will perform an add operation on the list. There should be no problem here, because what I am operating in the first for loop body is the size() of the second for loop. I remember a situation where for(){...} I directly operated the size() of for in... This seems to be impossible. There seemed to be some way at the beginning, but I can't think of it for the time being. Can any master Help me recall the memories? ?
Don’t use
for(:){}
, usefor(int i = 0; i < list.size(); i++)
, be careful not to create an infinite loopIn the for each, that is, the for(a : as) loop in your code, you cannot add or delete the looped collection, otherwise a ConcurrentModificationException will be reported.
There is no problem in this code. Because all your operations of changing the list are done outside the inner loop.
Depending on your needs is actually to count the data of each city by province. If you have the conditions to use Java 8, you can take a look at the GroupBy method of Stream. Can greatly simplify the code.
For some unrelated issues, the set method to set a list is not appropriate.
If it is an object with business logic, the internal structure should be packaged and provide an interface based on the concepts of the business domain, and the internal collection should not be directly exposed.
Even if it is a data transfer object, it should not provide the set method of the collection attribute. In general, the life cycle of a private collection variable should be managed by its parent object. The outside world operates the private collection through the add or remove method of the outer packaging object. If necessary, provide a collection get method. You need to consider whether to copy or make it immutable.
You cannot operate list (add, remove, etc.) in foreach
If you want to jump out of the two-level loop directly, there are only two ways:
Use the syntax of
break label
(I have never used this before, you can Google it);That’s the method you use, set a flag.
Also, the plural of
city
iscities
notcitys
.This is a long way to write. My words will be written like this:
Your original code has several areas that need optimization:
Try to avoid double loops, the inner loop should generally be extracted;
For operations on the same variable, the lines of code should be grouped together as much as possible, so that it reads more naturally;
Variable naming should reflect its business meaning and main type. For example, List objects should be named xxxList as much as possible. I'm too lazy to change this for you.
I don’t understand what you are asking..Is it because if can’t get in?
Organize your thoughts before asking questions. What do you want to ask? What answer do you hope to get? Otherwise, others won’t be able to answer it for you.