日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lambda分组集合中list和set区别

發布時間:2024/3/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。