一对一映射
List<Long> deliveryNoteIds = inboundParams.stream()
.map(PurchaseInboundUpdateDTO::getDeliveryNoteId).collect(Collectors.toList());
Map<Long, PurchaseDeliveryNote> deliveryNoteMap = deliveryNoteService.listByIds(deliveryNoteIds)
.stream().collect(Collectors.toMap(PurchaseDeliveryNote::getId, Function.identity()));
一对多映射(根据指定的字段)
Map<Long, List<String>> planIdShipCodeMap = planItemVOS.stream()
.filter(o -> StringUtil.isNotBlankAndNullString(o.getShipPlanCode()))
.collect(Collectors.groupingBy(PagePurchasePlanItemVO::getPurchasePlanId,
Collectors.mapping(PagePurchasePlanItemVO::getShipPlanCode, Collectors.toList())));
根据多个字段分组
Map<String, List<MskuSku>> mskuSkuMap = mskuSkuList.stream()
.collect(Collectors.groupingBy(o -> String.format("%s-%s", o.getMarketId(), o.getMsku())));
判断在集合中存在的元素个数
long count = shipPlanList.stream().filter(o -> !enableStatusList.contains(o.getStatus())).count();
避免空指针异常
Integer size = Optional.ofNullable(o.getItems()).map(List::size).orElse(0);
stream流排序
- 单字段排序
testList.stream().sorted(Comparator.comparing(User::getId));
testList.stream().sorted(Comparator.comparing(User::getId).reversed());
testList.stream().sorted(Comparator.comparing(User::getId, Comparator.reverseOrder()));
- 多字段排序
testList.stream().sorted(Comparator.comparing(User::getId).thenComparing(User::getNum));
testList.stream().sorted(Comparator.comparing(User::getId).reversed().thenComparing(User::getNum));
testList.stream().sorted(Comparator.comparing(User::getId, Comparator.reverseOrder()).thenComparing(User::getNum));
testList.stream().sorted(Comparator.comparing(User::getId).thenComparing(User::getNum).reversed());
testList.stream().sorted(Comparator.comparing(User::getId).thenComparing(User::getNum, Comparator.reverseOrder()));
testList.stream().sorted(Comparator.comparing(User::getId).reversed().thenComparing(User::getNum).reversed());
testList.stream().sorted(Comparator.comparing(User::getId, Comparator.reverseOrder()).thenComparing(User::getNum, Comparator.reverseOrder()));
欢迎各位大佬提供补全,谢谢!