ArrayList<MonthRecordRespVO> collects = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MonthRecordRespVO::getMonthRecord))), ArrayList::new));
时间: 2023-05-25 14:01:12 浏览: 129
这段代码是什么意思?
这段代码是将一个List转换为一个去重且按照月份排序的List。具体实现是通过使用Java 8中的流(stream)编程,利用Collectors工具类的静态方法进行操作。首先使用toCollection()方法创建一个TreeSet,按照MonthRecordRespVO对象的月份进行排序并去重。然后使用collectingAndThen()方法对排序后的结果进行转换为ArrayList格式的数据。最终得到的collects即为去重且按照月份排序的List。
相关问题
List<VIpiImageDetail> nodes = details.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(VIpiImageDetail::getMrmTypeName))), ArrayList::new));
这段代码使用了Java 8中的Stream API,将一个List集合中的元素按照VIpiImageDetail对象的mrmTypeName属性进行去重,并按照字典序排序后转换为一个ArrayList集合。
具体来说,这段代码将原始的List<VIpiImageDetail>类型的集合details转换为一个Stream流,然后使用collect()方法将其转换为一个集合。其中使用了两个参数,第一个参数是Collectors.collectingAndThen()方法,表示先使用toCollection()方法将Stream流中的元素存储到一个TreeSet集合中,其中Comparator.comparing()方法表示按照VIpiImageDetail对象的mrmTypeName属性进行比较排序。第二个参数是ArrayList::new,表示将TreeSet集合转换为一个ArrayList集合。
最终,nodes变量将存储去重、排序后的VIpiImageDetail对象列表。
// 分成 10 个线程并行处理数据 int threadNum = 10; int dataSizePerThread = awardIds.size() / threadNum; List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>(); for (int i = 0; i < threadNum; i++) { int startIndex = i * dataSizePerThread; int endIndex = (i == threadNum - 1) ? awardIds.size() : (i + 1) * dataSizePerThread; List<Long> subList = awardIds.subList(startIndex, endIndex); CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> { List<TCaAwardInfo> result = new ArrayList<>(); List<TCaAwardInfo> tCaAwardInfos = awardApiService.queryActivationCode(subList); result.addAll(tCaAwardInfos); return result; }, taskExecutor); futures.add(future); } // 等待所有线程执行完毕 CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); allFutures.join(); // 合并结果 List<TCaAwardInfo> activationCodeList = futures.stream() .flatMap(future -> future.join().stream()) .collect(Collectors.toList()); 优化一下这个代码
这段代码的并行处理数据的逻辑已经很好了,可以考虑在结果合并的时候进行优化。可以使用`CompletableFuture.join()`方法替代`CompletableFuture.join().stream()`,这样可以避免创建Stream对象,提高代码执行效率。同时,可以考虑使用`addAll()`方法替代`flatMap()`和`collect(Collectors.toList())`方法,从而避免创建Stream对象和List对象,提高代码执行效率。代码改进后如下:
```
int threadNum = 10;
int dataSizePerThread = awardIds.size() / threadNum;
List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
int startIndex = i * dataSizePerThread;
int endIndex = (i == threadNum - 1) ? awardIds.size() : (i + 1) * dataSizePerThread;
List<Long> subList = awardIds.subList(startIndex, endIndex);
CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> {
List<TCaAwardInfo> result = new ArrayList<>();
List<TCaAwardInfo> tCaAwardInfos = awardApiService.queryActivationCode(subList);
result.addAll(tCaAwardInfos);
return result;
}, taskExecutor);
futures.add(future);
}
List<TCaAwardInfo> activationCodeList = new ArrayList<>();
for (CompletableFuture<List<TCaAwardInfo>> future : futures) {
activationCodeList.addAll(future.join());
}
```
相关推荐
![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)