List<SubscribeOutBO> listAll = new ArrayList<>(); subLists.stream().forEach(e->{ if (e.getCityId() == null && e.getAreaId() ==null){ List<SubscribeOutBO> collect = subLists.stream().filter(s -> Objects.equals(s.getProvinceId(), provinceId)).collect(Collectors.toList()); listAll.add(collect);为什么后面listAll.add会报错
时间: 2024-04-23 13:25:42 浏览: 24
后面的listAll.add(collect)会报错,是因为collect的类型是List<SubscribeOutBO>,而listAll的类型是List< List<SubscribeOutBO>>,二者类型不一致,无法直接添加。如果你想将collect中的元素添加到listAll中,可以使用addAll方法,例如:listAll.addAll(collect)。这样就可以将collect中的元素全部添加到listAll中了。
相关问题
List<List<String>> allGroups = new ArrayList<List<String>>();转成List<String>allGroups
将二维列表 `List<List<String>> allGroups` 转换为一维列表 `List<String> allGroups` 可以使用双重循环遍历二维列表并将其元素添加到一维列表中,代码如下:
```java
List<String> allGroups = new ArrayList<String>();
for (List<String> group : allGroups) {
for (String item : group) {
allGroups.add(item);
}
}
```
这段代码首先遍历二维列表 `allGroups` 中的每一个分组 `group`,然后再遍历分组中的每一个元素 `item`,并将其添加到一维列表 `allGroups` 中。这样就可以将二维列表转换为一维列表了。
再精简优化下这个段代码 int batchCount = 500; int dataSizePerThread = Math.max(1, awardIds.size() / batchCount); List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>(); for (int i = 0; i < dataSizePerThread; i++) { int startIndex = i * batchCount; int endIndex = Math.min((i + 1) * batchCount, awardIds.size()); List<Long> subList = awardIds.subList(startIndex, endIndex); CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> { List<TCaAwardInfo> result = new ArrayList<>(); result = awardApiService.queryActivationCode(subList); return result; }, taskExecutor); futures.add(future); } List<TCaAwardInfo> activationCodeList = futures.stream() .flatMap(future -> future.join().stream()) .collect(Collectors.toList());
可以将for循环中的部分代码提取出来,简化代码如下:
int batchCount = 500;
int dataSizePerThread = Math.max(1, awardIds.size() / batchCount);
List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>();
for (int i = 0; i < dataSizePerThread; i++) {
int startIndex = i * batchCount;
int endIndex = Math.min((i + 1) * batchCount, awardIds.size());
List<Long> subList = awardIds.subList(startIndex, endIndex);
CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> awardApiService.queryActivationCode(subList), taskExecutor);
futures.add(future);
}
List<TCaAwardInfo> activationCodeList = futures.stream()
.flatMap(CompletableFuture::join)
.collect(Collectors.toList());
这里简化了supplyAsync方法中的Lambda表达式,将返回值直接作为Lambda表达式的返回值,避免了创建ArrayList对象。同时使用方法引用的方式替换了flatMap中的Lambda表达式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)