java 1.8 新特性 lambda 表达式 --- 项目中最常用的一些,后续会继续更新

废话不多说,直接上代码。

分组:
Map<String, List<Object>> collectList = list.stream().collect(Collectors.groupingBy(Object::obj));
for (Map.Entry<String, List<Object>> entry : collectList.entrySet()) {
     }

升序:
List<Object> collect = list.stream().sorted(Comparator.comparing(Object::obj).reversed()).collect(Collectors.toList());

降序:
List<Object> collect = list.stream().sorted(Comparator.comparing(Object::obj)).collect(Collectors.toList());

过滤并且返回对象:
Object= list().filter(a -> !StringUtils.isEmpty(a.obj()))
.sorted(Comparator.comparing(Object::obj).reversed()).findFirst().get();

max,min,sum,avg,count 
IntSummaryStatistics num = list.stream().mapToInt(u -> u.obj).summaryStatistics();

模糊: == like
List<Object>  list = returnList.stream().filter(d -> d.getName().contains(......)).collect(Collectors.toList());

分页:
List<Object> result = list.stream().skip(pageSize * (pageNum - 1)).limit(pageSize).collect(Collectors.toList());

过滤:
List<Object> collect = arrayList.stream().filter(x -> x.obj().equals(......)).collect(Collectors.toList());

过滤是否有包含的数据:
List<String> collect = range.stream().filter(str -> !list.contains(str)).collect(Collectors.toList());

集合返回某个字段
List<String> xjNo = entry.getValue().stream().map(Object::obj).collect(Collectors.toList());

BigDecimal 求和 等...
BigDecimal salesAmount = list.stream()
                    // 将 InvStockStatisticsResult 对象的 salesAmount 取出来map为Bigdecimal
                    .map(Object::obj)
                    // 使用reduce聚合函数,实现累加器
                    .reduce(BigDecimal.ZERO,BigDecimal::add);

BigDecimal  求平均:
BigDecimal invoiceRate = list.stream().map(Object::obj).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(list.size()), 2, BigDecimal.ROUND_HALF_UP);

list 集合对象中去重
List<Object> collect = list.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Object::obj))), ArrayList::new)
            );

你可能感兴趣的:(lambda,java)