优化代码Set<String> rdsSet = new HashSet<>(); Set<String> empSet = new HashSet<>(); int rdsPageNum = 1; int empPageNum = 1; boolean rdsFinished = false; boolean empFinished = false; while (!rdsFinished || !empFinished) { if (!rdsFinished) { PageHelper.startPage(rdsPageNum, 1000); List<String> accountCode = idmEmployeeMapper.getaccountCode(); if (accountCode.isEmpty()) { rdsFinished = true; } else { rdsSet.addAll(accountCode); rdsPageNum++; } } if (!empFinished) { PageHelper.startPage(empPageNum, 1000); List<String> userId = idmEmployeeMapper.getEmployeeUserId(); if (userId.isEmpty()) { empFinished = true; } else { empSet.addAll(userId); empPageNum++; } } } List<String> result = new ArrayList<>(empSet); result.removeAll(rdsSet);
时间: 2024-03-05 20:50:42 浏览: 163
可以考虑将两个while循环合并成一个,减少重复代码。同时,可以使用Set的addAll方法来添加元素,避免每次添加都需要遍历一遍。最终代码可优化为:
```
Set<String> rdsSet = new HashSet<>();
Set<String> empSet = new HashSet<>();
int rdsPageNum = 1;
int empPageNum = 1;
boolean rdsFinished = false;
boolean empFinished = false;
while (!rdsFinished || !empFinished) {
List<String> accountCode = new ArrayList<>();
List<String> userId = new ArrayList<>();
if (!rdsFinished) {
PageHelper.startPage(rdsPageNum, 1000);
accountCode = idmEmployeeMapper.getaccountCode();
if (accountCode.isEmpty()) {
rdsFinished = true;
} else {
rdsSet.addAll(accountCode);
rdsPageNum++;
}
}
if (!empFinished) {
PageHelper.startPage(empPageNum, 1000);
userId = idmEmployeeMapper.getEmployeeUserId();
if (userId.isEmpty()) {
empFinished = true;
} else {
empSet.addAll(userId);
empPageNum++;
}
}
// 如果两个列表都为空,则跳出循环
if (accountCode.isEmpty() && userId.isEmpty()) {
break;
}
}
List<String> result = new ArrayList<>(empSet);
result.removeAll(rdsSet);
```
阅读全文