将下列for循环转换成递归 // 查询所有省 List<CityVo> provinceList = regionMapper.findAllProvince(); // 查询所有市 List<CityVo> cityList = regionMapper.findAllCity(); // 查询所有区县 List<CityVo> areaList = regionMapper.findAllArea(); // 查询所有街道 List<CityVo> streetList = regionMapper.findAllStreet(); List<RegionVo> compensationList = new ArrayList<>(); List<RegionVo> list = new ArrayList<>(); // 组装数据 if (CollectionUtil.isNotEmpty(provinceList)){ for (CityVo province : provinceList) { RegionVo provinceVo = new RegionVo() .setLabel(province.getName()) .setValue(province.getId().toString()); if (CollectionUtil.isNotEmpty(cityList)){ List<RegionVo> citys = new ArrayList<>(); for (CityVo city : cityList) { if (province.getId().equals(city.getPId())){ RegionVo cityVo = new RegionVo() .setValue(city.getId().toString()) .setLabel(city.getName()); if (CollectionUtil.isNotEmpty(areaList)){ List<RegionVo> areas = new ArrayList<>(); for (CityVo area : areaList) { if (area.getPId().equals(city.getId())){ RegionVo areaVo = new RegionVo() .setValue(area.getId().toString()) .setLabel(area.getName()); if (CollectionUtil.isNotEmpty(streetList)){ List<RegionVo> streets = new ArrayList<>(); for (CityVo street : streetList) { if (street.getPId().equals(area.getId())){ RegionVo streetVo = new RegionVo() .setValue(street.getId().toString()) .setLabel(street.getName()); streets.add(streetVo); } } if (CollectionUtil.isEmpty(streets)){ compensationList.add(areaVo); } areaVo.setChildren(streets); } areas.add(areaVo); } } cityVo.setChildren(areas); } citys.add(cityVo); } } provinceVo.setChildren(citys); } list.add(provinceVo); }
时间: 2023-08-03 09:04:29 浏览: 91
以下是将原始的for循环转换成递归的代码示例:
```
// 查询所有省
List<CityVo> provinceList = regionMapper.findAllProvince();
// 查询所有市
List<CityVo> cityList = regionMapper.findAllCity();
// 查询所有区县
List<CityVo> areaList = regionMapper.findAllArea();
// 查询所有街道
List<CityVo> streetList = regionMapper.findAllStreet();
List<RegionVo> compensationList = new ArrayList<>();
List<RegionVo> list = new ArrayList<>();
// 递归函数,用于组装省、市、区县和街道信息
private void assembleData(List<CityVo> provinceList, List<CityVo> cityList, List<CityVo> areaList, List<CityVo> streetList, List<RegionVo> compensationList, List<RegionVo> list) {
for (CityVo province : provinceList) {
RegionVo provinceVo = new RegionVo().setLabel(province.getName()).setValue(province.getId().toString());
List<RegionVo> citys = new ArrayList<>();
for (CityVo city : cityList) {
if (province.getId().equals(city.getPId())) {
RegionVo cityVo = new RegionVo().setValue(city.getId().toString()).setLabel(city.getName());
List<RegionVo> areas = new ArrayList<>();
for (CityVo area : areaList) {
if (area.getPId().equals(city.getId())) {
RegionVo areaVo = new RegionVo().setValue(area.getId().toString()).setLabel(area.getName());
List<RegionVo> streets = new ArrayList<>();
for (CityVo street : streetList) {
if (street.getPId().equals(area.getId())) {
RegionVo streetVo = new RegionVo().setValue(street.getId().toString()).setLabel(street.getName());
streets.add(streetVo);
}
}
if (CollectionUtil.isEmpty(streets)) {
compensationList.add(areaVo);
} else {
areaVo.setChildren(streets);
}
areas.add(areaVo);
}
}
if (CollectionUtil.isNotEmpty(areas)) {
cityVo.setChildren(areas);
citys.add(cityVo);
}
}
}
if (CollectionUtil.isNotEmpty(citys)) {
provinceVo.setChildren(citys);
list.add(provinceVo);
}
}
}
// 调用递归函数组装数据
assembleData(provinceList, cityList, areaList, streetList, compensationList, list);
```
需要注意的是,递归函数应该在外部调用时进行初始化,并保证所有参数都传入递归函数中。在本例中,递归函数的参数包括四个List类型的数据、两个用于保存结果的List以及一个用于保存补偿数据的List。递归函数的作用是根据传入的数据列表组装成对应的省、市、区县和街道信息,并将结果保存到结果列表中。
阅读全文