今天修改个东西,看到有一段代码被人优化了!!!
优化前
public Optional<List<Map<String, String>>> getByCodes(String type, List<String> codeList) {
if (codeList.size() <= DEFAULT_PAGE_SIZE) {
return getByBatch(type, codeList);
}
return IntStream.range(0, (codeList.size() + DEFAULT_PAGE_SIZE - 1) / DEFAULT_PAGE_SIZE)
.mapToObj(i -> codeList.subList(i * DEFAULT_PAGE_SIZE, Math.min(DEFAULT_PAGE_SIZE * (i + 1), codeList.size())))
.map(list -> getByBatch(type, list).orElse(new ArrayList<>()))
.reduce((a, b) -> {
a.addAll(b);
return a;
});
}
private Optional<List<Map<String, String>>> getByBatch(String type, List<String> codeList) {}
优化后
public Optional<List<Map<String, String>>> getByCodes(String type, List<String> codeList) {
Optional<List<Map<String, String>>> result = null;
if (codeList.size() <= DEFAULT_PAGE_SIZE) {
result = getByBatch(type, codeList);
if(result.isPresent() && CollectionUtils.isNotEmpty(result.get())){
return result;
}
return Optional.empty();
}
result = IntStream.range(0, (codeList.size() + DEFAULT_PAGE_SIZE - 1) / DEFAULT_PAGE_SIZE)
.mapToObj(i -> codeList.subList(i * DEFAULT_PAGE_SIZE, Math.min(DEFAULT_PAGE_SIZE * (i + 1), codeList.size())))
.map(list -> getByBatch(type, list).orElse(new ArrayList<>()))
.reduce((a, b) -> {
a.addAll(b);
return a;
});
if(result.isPresent() && CollectionUtils.isNotEmpty(result.get())){
return result;
}
return Optional.empty();
}
private Optional<List<Map<String, String>>> getByBatch(String type, List<String> codeList) {}