stream流

去重

1
2
3
4
List<String> orderNoList = inOutWarehouseRecordMap.values().stream()
.map(InOutWarehouseRecord::getOrderNo)
.distinct()
.collect(Collectors.toList());

将value为集合的map转化为list

1
2
//Map<Long, List<RecordGoodsDetail>> goodsDetailMap
List<RecordGoodsDetail> allDetails = goodsDetailMap.values().stream() .flatMap(List::stream).collect(Collectors.toList());

常用构造方式

1
2
3
4
5
List<SuppliedMaterialsDTO> suppliedMaterialsDTOList = records.stream().map(inOutWarehouseRecord -> {
return SuppliedMaterialsDTO.builder()
//......
.build();
}).collect(Collectors.toList());

根据集合的某一个字段(比如id),进行分类,计算其中的几个属性累加值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//相同的进行累加
Map<Long, OrderReturnMaterialsDTO> mergedMap = orderReturnMaterialsDTOList.stream()
.collect(Collectors.toMap(
OrderReturnMaterialsDTO::getGoodsDetailId,
dto -> dto,
(dto1, dto2) -> OrderReturnMaterialsDTO.builder()
.goodsDetailId(dto1.getGoodsDetailId())
.categorize(dto1.getCategorize())
.type(dto1.getType())
.name(dto1.getName())
.goodsModel(dto1.getGoodsModel())
.returnNumber(dto1.getReturnNumber().add(dto2.getReturnNumber()))
.build()
));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//相同的进行累加
Map<Long, OrderSuppliedMaterialsDTO> mergedMap = orderSuppliedMaterialsDTOList.stream()
.collect(Collectors.toMap(
OrderSuppliedMaterialsDTO::getGoodsDetailId,
dto -> dto,
(dto1, dto2) -> OrderSuppliedMaterialsDTO.builder()
.id(dto1.getId())
.goodsDetailId(dto1.getGoodsDetailId())
.categorize(dto1.getCategorize())
.type(dto1.getType())
.name(dto1.getName())
.goodsModel(dto1.getGoodsModel())
.receiveNumber(dto1.getReceiveNumber().add(dto2.getReceiveNumber()))
.returnNumber(dto1.getReturnNumber().add(dto2.getReturnNumber()))
.transferInNumber(dto1.getTransferInNumber().add(dto2.getTransferInNumber()))
.transferOutNumber(dto1.getTransferOutNumber().add(dto2.getTransferOutNumber()))
.build()
));