1.求和
LinkedHashMap<String, Double> countMap = hourList.stream().collect(Collectors.groupingBy(o ->o.getCheckDate() + "-" + o.getCheckHour(), LinkedHashMap::new,
Collectors.summingDouble(s->{
EnergyTypeEnum type = EnergyTypeEnum.getByValue(s.getEnergyType());
return EnergyTypeIdList.CalcConsume(type,s.getAmount());
List<EnergyConsumeHour> hourList1 = countMap.keySet().stream().map(key -> {
String[] temp = key.split("-");
String checkHour = temp[1];
String checkDate = temp[0];
EnergyConsumeHour record = new EnergyConsumeHour();
record.setCheckDate(checkDate);
record.setCheckHour(Integer.valueOf(checkHour));
record.setAmount(countMap.get(key));
return record;
}).collect(Collectors.toList());2
2.求平均
Map<Integer, Double> lisXMap3 = list.stream().collect(Collectors.groupingBy(AnalyseVo::getCheckHour,
Collectors.averagingDouble(AnalyseVo::getAmount)));
3.Map
Map<Integer, Double> lisXMap2 = list.stream().filter(one -> one.getDateDesc().equals(lastDay))
.collect(Collectors.toMap(AnalyseVo::getCheckHour, AnalyseVo::getAmount));
return list.stream()
.collect(Collectors.groupingBy(SysProjectGroupVo::getGroupLevel,
Collectors.mapping(SysProjectGroupVo::getOpenId, Collectors.joining(","))));
4. reduce 求和
BigDecimal reduce = children.stream().map(line ->
Optional.ofNullable(line.getInboundQuantity()).orElse(BigDecimal.ZERO))
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal sum = lineList.stream().map(DtwmsPoInventoryLineBo::getDiscountTaxAmount).reduce(BigDecimal.ZERO, (acc, item)->{
return acc.add(item);
});
BigDecimal totalAmount = list.stream().map(x -> x.getPrice().multiply(x.getSaleQty())).reduce(BigDecimal.ZERO, BigDecimal::add);
// BigDecimal prodRate = list.stream().map(x -> x.getProdRate()).reduce(BigDecimal.ZERO, BigDecimal::add);
5.peek
pageData.getRecords().stream().peek(data -> {
data.setBmsOrderStatus(getBMSOrderStatus(data, finalStatusModeMap,null,null));
}).collect(Collectors.toList());
boolean present = roleList.stream().filter(a -> a.getRoleId().equals(req.getRoleId())).findAny().isPresent();
boolean present = roleList.stream().anyMatch(a -> a.getRoleId().equals(req.getRoleId()));
说明:
anyMatch():匹配到任何一个元素和指定的元素相等,返回 true
allMatch():匹配到全部元素和指定的元素相等,返回 true
noneMatch():与 allMatch() 效果相反
Map<String, List<Working>> map =
workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
e -> {
ArrayList<Working> list = new ArrayList<>();
list.add(e);
return list;
},
(oldList, newList) -> {
oldList.addAll(newList);
return oldList;
}));
// 或者使用groupBy
// 存为List
Map<String, List<BusinessSceneDetail>> collect0 =
sceneDetailMap.values().stream().collect(
Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode));
// 存为set
Map<String, Set<BusinessSceneDetail>> collect =
sceneDetailMap.values().stream().collect(
Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode, Collectors.toSet()));
// 多层map
Map<String, Map<String, Set<BusinessSceneDetail>>> collect1 = sceneDetailMap.values().stream().collect(
Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode, Collectors.groupingBy
(BusinessSceneDetail::getBusinessSceneCode, Collectors.toSet())));
// 对象子属性 map
Map<String, List<String>> collect2 = sceneDetailMap.values().stream().collect(Collectors.groupingBy
(BusinessSceneDetail::getDataSourceCode, Collectors.mapping(BusinessSceneDetail::getRuleContent,
Collectors.toList())));
// 对象按照规则获取一个
Map<String, BusinessSceneDetail> collect3 =
sceneDetailMap.values().stream().collect(Collectors.groupingBy
(
BusinessSceneDetail::getDataSourceCode,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)),
Optional::get
)
));
// 对象按照规则获取一个里面的值 p肯定存在
Map<String, String> collect4 = sceneDetailMap.values().stream().collect(Collectors.groupingBy
(
BusinessSceneDetail::getDataSourceCode,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)),
p -> p.get().getRuleContent()
)
));
// map的value进行排序
Map<String, List<SysDictionary>> map = sysDictionaryList.stream().collect(
Collectors.groupingBy(
SysDictionary::getSysCode,
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>((Comparator<Object>) (o1, o2) -> 0)),
ArrayList::new
)
)
);
// map的key进行排序
Map<String, List<SysDictionary>> map1 = sysDictionaryList.stream().collect(
Collectors.groupingBy(
SysDictionary::getSysCode,
TreeMap::new,
Collectors.toList()
)
);