一、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())));

二、按条件筛选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));

 [{"0":"a","1":"b"},{"0":"a1","1":"b1"}] -> {a:b,a1:b2}

相关文章: