stream()日常使用记录

101 阅读1分钟

排序 通过sorted排序也可在comparing后添加参数Comparator.reverseOrder()进行倒叙

list.stream().sorted(Comparator.comparing(BurdenPlanCommon::getCreateDate))

过滤

list.stream().filter(coal -> coal.getName() != null && !"空".equals(coal.getName()))

合并统计、根据name相同的合并,并将val相加,再取map的value转换回list

list.stream().collect(Collectors.toMap(MatchCoal::getName, coal -> coal, (coal1, coal2) -> {
    coal1.setVal(coal1.getVal()+(coal2.getVal()));
    return coal1;
})).values().stream().collect(Collectors.toList());

最值获取

double val = list2.stream().max(Comparator.comparing(MatchCoal::getVal)).get().getVal();

持续更新中......