JAVA8:stream流
List<SystemCodeDataDTO> systemCodeDataDTOList = Optional.ofNullable(data).map(JSON::parseArray).map(d -> d.toJavaList(SystemCodeDataDTO.class)) .map(List::stream).map(s -> s.filter(SystemCodeDataDTO::getProductLevel)).map(s -> s.collect(Collectors.toList())).orElseGet(ArrayList::new);
一、List对象拆分
//1. 根据对象属性进行分组 Map<String, List<Order>> orderMap = orderListAll.stream().collect(Collectors.groupingBy(Order::getProdProductId)); //2. 根据对象中的对象属性进行分组 Map<String, List<BagItem>> orderMap = bagItemList.stream().collect(Collectors.groupingBy(bagItem -> bagItem.getBag().getId())); //3. 根据对象属性进行分组,且只映射出对象某个属性的列表 Map<String, List<String>> orderClearanceIdMap = orderClearanceAllList.stream().collect(Collectors.groupingBy(OrderClearance::getValuationType, Collectors.mapping(OrderClearance::getId, Collectors.toList()))); //4. 分组后排序取最新测试时间的记录 Map<String, CalibrateCheckResult> calibrateCheckResultMap = calibrateCheckResultList.stream().collect(Collectors.groupingBy(i -> i.getProcess(), Collectors.collectingAndThen(Collectors.reducing((c1, c2) -> c1.getTestTime().compareTo(c2.getTestTime()) >= 0 ? c1 : c2), Optional::get)));
二、按条件筛选List
//按条件筛选 List list = list.stream().filter(l -> l.getName().equals("x")).collect(Collectors.toList()); //是否存在满足条件的元素 anyMatch:任意一个 allMatch:所有 noneMatch:没有一个 Boolean b = list.stream().anyMatch(clearGoods -> clearGoods.getName().equals("a"));
三、将对象某个属性转List
List<String> idList = logisticsFinanceDayOrderList.stream().map(LogisticsFinanceDayOrder::getId).collect(Collectors.toList());
四、将对象某个属性拼接为String
String chargeTypeStr = financeOrderItemExpenses.stream().map(FinanceOrderItemExpense::getChargeType).distinct().collect(Collectors.joining(","));
五、对象属性加减乘除
//将对象某个属性(Integer)求和 Integer count = customerGoodsMessageVOList.stream().mapToInt(CustomerGoodsMessageVO::getCount).sum(); //将对象某个属性(Float)求和 Float totalWeight = financeOrderList.stream().map(CustomerFinanceOrder::getWeight).reduce(Float::sum).get(); //将对象某个属性(BigDecimal)求和 BigDecimal totalPrice1 = financeOrderList.stream().map(CustomerFinanceOrder::getBeforeDiscountPrice).reduce(BigDecimal::add).get();
六、List转Map
Map<String, String> map = platformIncomeWithdrawFianceHistoryDao.getDistinctAccountType().stream().collect(Collectors.toMap(String::toString, s -> s));
Map<String, String> map = list.stream().collect(Collectors.toMap(item -> item.get("value"), item -> item.get("label")));
七、Map转List
//将Map的Key转成List List passIdList = map.entrySet().stream().filter(s -> s.getValue().equals("a")).map(Map.Entry::getKey).collect(Collectors.toList());
八、按对象某个属性去重
List<ChannelApplication> unique = channelApplicationList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ChannelApplication::getApplicationName))), ArrayList::new));
九、普通操作
//循环添加 exportExcelTemplateItemList.stream().forEach(exportExcelTemplateItem -> exportList.add(exportExcelTemplateItem));
//每个元素均执行某个方法
List<AddOrUpdatePluginInfoVO> addOrUpdatePluginInfoVOList = records.stream().map(this::convertOne).collect(Collectors.toList());
//元素拆分后合并成List
List<Long> mediumIdList = channelPromotionVOList.stream().map(ChannelPromotionVO::getMediumId).distinct().map(m -> StrUtil.split(m, ",")).flatMap(Arrays::stream).filter(StrUtil::isNotEmpty).map(Long::parseLong).collect(Collectors.toList());
十、其它
dataGrid.getRows().stream().skip((pageNo - 1) * pageSize).limit(pageSize).forEach(e -> currentPageData.add(e));
//先根据“条件优先级配置”的优先级分组,再排序获取优先级最高,再根据“产品谱系”中的层级优先级排序(产品型号 < 产品系列 < 产品类型)
StepWarningStandard stepWarningStandard = stepWarningStandardList
.stream().collect(Collectors.groupingBy(i -> i.getPriorityElementConfig().getPriority())).entrySet()
.stream().sorted(Map.Entry.<Integer, List<StepWarningStandard>>comparingByKey()).findFirst().get().getValue()
.stream().sorted(Comparator.comparing(i -> i.getPedigree().getType())).reduce((first, second) -> second).get();
[{"0":"a","1":"b"},{"0":"a1","1":"b1"}] -> {a:b,a1:b2}