lambda分组集合中list和set区别
生活随笔
收集整理的這篇文章主要介紹了
lambda分组集合中list和set区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
集合List和Set使用
問題
多線程 處理數據,每個子線程返回的結果都一樣。理論上返回結果集應該不一樣。
排查
檢查線程中處理邏輯是否有問題,InsuranceDetailCallable中call()方法主體邏輯正確,可能存在的傳到每個子線程中的List insuranceDetails數據是一樣的,導致每個線程的數據結果都一樣。
/*** 數據處理 operate_type link_id*/ private class InsuranceDetailCallable implements Callable<Set<Long>> {private CountDownLatch countDownLatch;private List<FinalStatementInsuranceDetail> insuranceDetails;public InsuranceDetailCallable(CountDownLatch countDownLatch,List<FinalStatementInsuranceDetail> insuranceDetails) {this.countDownLatch = countDownLatch;this.insuranceDetails = insuranceDetails;}@Overridepublic Set<Long> call() {Set<Long> result = new HashSet<>();try {//代碼省略} catch (Exception e) {System.out.println("子線程異常:" + e.getMessage());} finally {countDownLatch.countDown();}return result;} }父線程中將獲取到的list數據根據statementId字段進行分組得到Map集合,接著將Map中的key進行分組得到List<List>組,子線程分別對這些group中集合進行處理數據。問題出現在代碼
List<List<Long>> groupList = Stream.iterate(0, n -> n + 1).limit(set.size()).parallel().map(a -> set.stream().skip((long) a * finalSize).limit(finalSize) .parallel().collect(Collectors.toList())).filter(CollUtil::isNotEmpty).collect(Collectors.toList());處理的集合Set是無序的,并行處理分組數據,導致分組完的groupList中數據List部分數據一樣,子線程中處理的數據也一樣。List是有序的,將Set轉換成List,可以解決這個問題。
/*** 修改前父線程代碼*/ public void testMaintainNormalOperateType() {//獲取還未處理的數據List<FinalStatementInsuranceDetail> insuranceDetailList = insuranceDetailService.listOperateTypeIsNull();if (CollUtil.isEmpty(insuranceDetailList)) {return;}Map<Long, List<FinalStatementInsuranceDetail>> mapList = insuranceDetailList.stream().collect(Collectors.groupingBy(FinalStatementInsuranceDetail::getStatementId));Set<Long> set = mapList.keySet();int size = set.size() / 100;if (size == 0) {size = set.size();}int finalSize = size;List<List<Long>> groupList = Stream.iterate(0, n -> n + 1).limit(list.size()).parallel().map(a -> set.stream().skip((long) a * finalSize).limit(finalSize).parallel().collect(Collectors.toList())).filter(CollUtil::isNotEmpty).collect(Collectors.toList()); }修改后的父線程代碼
public void testMaintainNormalOperateType() {long start = System.currentTimeMillis();List<FinalStatementInsuranceDetail> insuranceDetailList = insuranceDetailService.listOperateTypeIsNull(true);if (CollUtil.isEmpty(insuranceDetailList)) {return;}Map<Long, List<FinalStatementInsuranceDetail>> mapList = insuranceDetailList.stream().collect(Collectors.groupingBy(FinalStatementInsuranceDetail::getStatementId));List<Long> list = new ArrayList<>(mapList.keySet());try {int size = list.size() / 100;if (size == 0) {size = list.size();}int finalSize = size;List<List<Long>> groupList = Stream.iterate(0, n -> n + 1).limit(list.size()).parallel().map(a -> list.stream().skip((long) a * finalSize).limit(finalSize).parallel().collect(Collectors.toList())).filter(CollUtil::isNotEmpty).collect(Collectors.toList());ExecutorService executorService = Executors.newFixedThreadPool(groupList.size());CountDownLatch countDownLatch = new CountDownLatch(groupList.size());List<Future<Set<Long>>> futureList = new ArrayList<>();for (List<Long> statementIdList : groupList) {List<FinalStatementInsuranceDetail> insuranceDetails = new ArrayList<>();for (Long statementId : statementIdList) {insuranceDetails.addAll(mapList.getOrDefault(statementId, new ArrayList<>()));}InsuranceDetailCallable callable = new InsuranceDetailCallable(countDownLatch, insuranceDetails);Future<Set<Long>> future = executorService.submit(callable);futureList.add(future);}executorService.shutdown();for (Future<Set<Long>> future : futureList) {Set<Long> setLong = future.get();System.out.println("輸出:" + setLong.size() + " " + JSONObject.toJSONString(setLong));}} catch (Exception e) {System.out.println("父線程異常:" + e.getMessage());}long end = System.currentTimeMillis();System.out.println("耗時 = " + (end - start) / 1000); }另外一種方式使用apache.commons夾包ListUtils.partition(List list, int size)方法
<!--對應maven依賴 --> <dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.1</version> </dependency>總結
List是有序的,不唯一;Set無序,數據唯一。
總結
以上是生活随笔為你收集整理的lambda分组集合中list和set区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: reboot无法进入grub开机选单
- 下一篇: 软件测试之与大厂测试经理的问答