JAVA8 常用Stram处理方法
- 排序
- [排序对象集合属性一升序 属性二降序](#排序对象集合属性一升序 属性二降序)
-
排序
List<MachineOrderResponse.BackRecord> noSList = ss.stream().sorted(Comparator.comparing(MachineOrderResponse.BackRecord::getTime).reversed()).collect(Collectors.toList());
排序对象集合属性一升序 属性二降序
implicationHolderTableTemplateDtoList.sort(Comparator.comparing(ImplicationHolderTableTemplateDto::getaBDistinguish).thenComparing(ImplicationHolderTableTemplateDto::getAttendCnt, Comparator.reverseOrder()));
List转Map
Map<BigDecimal, String> collect = probabilitiesList.stream().collect(Collectors.toMap(ll -> BigDecimal.valueOf(ll.getADouble()), PlayResponse.Probability::getUuid));
List分组
Map<String, List<MachineOrderResponse.GoodsOrder>> goodMap = leaveList.stream().collect(Collectors.groupingBy(MachineOrderResponse.GoodsOrder::getProduct_id));
求和
int totalGoodsCount = goodsLists.stream().mapToInt(PostageSettingRequest.GoodsList::getGoodsListCount).sum();
提取字符串
String goodsName = orderGoodsList.stream().map(goods -> goods.getGoodsName() + " X " + goods.getOrderGoodsCount())
.collect(Collectors.joining(", "));
根据属性去重
mandataryInfoTableList = mandataryInfoTableList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(mandataryInfoTable -> mandataryInfoTable.getCertificateId() + ";" + mandataryInfoTable.getHolderName()))), ArrayList::new));
分组排序
Map<String, List<FuzzyTrialCalculationDto>> groupMap = fuzzyTrialCalculationDtoList.stream()
.sorted(Comparator.comparingInt(FuzzyTrialCalculationDto::getAlgorithmSort))
.collect(Collectors.groupingBy(FuzzyTrialCalculationDto::getAlgorithmName, LinkedHashMap::new, Collectors.toList()));
求和
int totalAge = peopleList.stream()
.mapToInt(Person::getAge)
.sum();