jdk1.8日常使用示例
//1、List转Map
List<RegionVO> listVO = Lists.newArrayList();
//todo赋值
Map<String, RegionVO> resultMap = listVO.stream().collect(Collectors.toMap(RegionVO::getId, Function.identity()));
//2、List转Map,分组
List<ContractPathDTO> listPath=Lists.newArrayList();
//todo赋值
Map<String, List<ContractPathDTO>> mapPath = listPath.stream().collect(Collectors.groupingBy(ContractPathDTO::getStartEndKey));
List<AbstractObjectInfo> abstractObjectInfos =Lists.newArrayList();
//todo赋值
Map<Long, AbstractObjectInfo> objectInfoMap = abstractObjectInfos.stream()
.collect(StreamCollectors.toHashMap(AbstractObjectInfo::getAbstractObjectUkid, Function.identity()));
//3、List转Set
List<StorageView> storageViews = Lists.newArrayList();
//todo赋值
Set<Long> businessIds = storageViews.stream().map(StorageView::getGoodsOwnerUkid).collect(Collectors.toSet());
4、List 求和(整数)
List<SwStorageDetail> swStorageDetails = Lists.newArrayList();
//todo赋值
//商品总数
long goodsTotal = swStorageDetails.stream().mapToLong(SwStorageDetail::getGoodsAmount).sum();
//5、List 求和(整数)BigDecimal 类型数据求和,使用reduce
参考https://www.cnblogs.com/yangzhenlong/p/8378029.html
BigDecimal add = list.stream().map(User::getHeight).reduce(BigDecimal.ZERO, BigDecimal::add);
//6. 过滤出所有参数类型为入参的参数
List<GwServiceParamDTO> iNParamList = gwServiceParamDTOList.stream()
.filter(a -> a.getParamInoutype() == IN_PARAM_TYPE).collect(Collectors.toList());
//7.Map赋值
Map<Long, Set<ContainerDo>> contractContainerMap = new HashMap<>(16);
for (.....) {
Long contractUkid = ...;
ContainerDo container = ...;
contractContainerMap.computeIfAbsent(contractUkid, k -> new HashSet<>()).add(container);
}
//8.取最值
//取最长备注
String remark = seTransferOrders.stream().max((o1, o2)-> {
String name1 = Optional.ofNullable(o1.getRemark()).orElse("");
String name2 = Optional.ofNullable(o2.getRemark()).orElse("");
return name1.length() > name2.length() ? 1 : name1.length() == name2.length() ? 0 : -1;
}).get().getRemark();
// 9.抛异常
Long relationUkid = Optional.ofNullable(imSubItemService.getBySubItemUkid(relationUkid)).map(ImSubItem::getItemUkid).orElseThrow(NOT_FOUND_GROUP);
// 10.过滤
if (relations.stream().anyMatch((a) -> a.getStepFlow() < 5)) {
........
}