原文地址
原文链接
初始化
Arrays.asList(1, 2, 3);
new ArrayList<>(Arrays.asList(1, 2, 3));
new LinkedList<>(Arrays.asList(1, 2, 3));
Arrays.stream(new int[]{1,3,4}).boxed().collect(Collectors.toList());
Point[] pointArray = new MultiPoint(points.toArray(new Point[0]);
求和
dto.setSaleNum(orderMessageDtoList.stream().mapToInt(OrderMessageDto::getNum).sum());
BigDecimal totalPrice = list.stream().map(Apple::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
分组
List<FamilyBookDeliveryCardEntity> allCardList = source.getAllCardList();
Map<Long, List<FamilyBookDeliveryCardEntity>> parentToCardMap =
allCardList.stream().collect(Collectors.groupingBy(FamilyBookDeliveryCardEntity::getParentCardId));
Map<String, List<HouseDto>> villageHouseMap
= houseDtoList.stream().collect(Collectors.groupingBy(cube ->
cube.getDivisionFullName() + "_" + cube.getStreet() + "_" + cube.getVillage()));
除去特定对象
cubeItemBoList.removeIf(cubeItemBO -> cubeItemBO.getStatus().equals(ItemStatusEnum.DISABLED.getValue()));
查询,正向条件
List<FamilyBookDeliveryCardEntity> childCardEntity = entityList.stream()
.filter(entity -> parentEntity.getId().equals(entity.getParentCardId()))
.collect(Collectors.toList());
查重
if (optList.size() != optList.stream().distinct().count()) {
throw ErrorCode.BAD_PARAM.message("选项元素不能重复").exception();
}
去重
List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
List<SkuPropertyEntity> skuPropertyEntityListAlone = skuPropertyEntityList.stream().
collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(SkuPropertyEntity::getPropertyId))), ArrayList::new));
List<PromotionSkuModel> promotionSkuModelDistinctList = promotionSkuModelList
.stream()
.collect(Collectors
.collectingAndThen(Collectors
.toCollection(()
-> new TreeSet<>(Comparator
.comparing(o -> o.getPromotionId() + ";" + o.getSkuId()))), ArrayList::new));
排序
List<CubeSessionItemBO> cubeSessionItemBoSortList = cubeSessionItemBOList.stream().sorted(Comparator.comparing(CubeSessionItemBO::getItemId)).collect(Collectors.toList());
Optional<FamilyBookDeliveryCardEntity> maxInvalidOptional =
entityList.stream().max(Comparator.comparing(FamilyBookDeliveryCardEntity::getInvalidTime));
return cardBaseMessageVoList.stream().sorted(Comparator
.comparing(CardBaseMessageVo::getExpireTime)
.thenComparing(CardBaseMessageVo::getStatus, Comparator.reverseOrder())
.thenComparing(CardBaseMessageVo::getId))
.collect(Collectors.toList());
插入
String result = Joiner.on(",").join(skuList);
对象转换
List<CartDTO> cartDTOList = new ArrayList<>();
for (OrderDetail item : orderDetailList){
cartDTOList.add(new CartDTO(item.getProductId(),item.getProductQuantity()));
}
List<CartDTO> cartDTOList = orderDetailList.stream()
.map(e -> new CartDTO(e.getProductId(), e.getProductQuantity()))
.collect(Collectors.toList());
List<Long> productIdList = orderDetailList.stream().map(CartDTO::getProductId).collect(Collectors.toList()));
Map<String, SysDictType> map = typeList.stream().collect(
Collectors.toMap(SysDictType::getId, Function.identity() ,(key1 , key2)-> key2 ));
Map<String, List<HrHousePrAudit>> map = hrHousePrAuditList.stream().collect(
Collectors.groupingBy(HrHousePrAudit::getHousePrId));
Map<String, List<UnpushedRoomResults>> dataMapGroupByBuild = dataList.stream()
.collect(Collectors.groupingBy(data-> data.getBuildingNum()+data.getUnit()));
其他流处理
stream
原文地址
原文链接