Stream流用法

public class test{
List<entity> list=new ArrayList();
list.stream().collect(Collectors.toMap(x -> TimeUtil.getMonth(x.getMonth()), x -> x));
//以月份为key,截取list里面的月份

list.stream().
        filter(x -> x.getType() != null).
        collect(Collectors.groupingBy(x-> x.getType().substring(0,3), Collectors.summingInt(entity::getNum)));
//过滤后截取在计算


int sum = countPersonAgeDtos.stream().mapToInt(CountPersonAgeDto::getNum).sum();
//计算某个字段的总计


entity lastProgress = list.stream().max(Comparator.comparing(Entity::getTime)).get();
//获得list里面时间最大的对象


list.stream().filter(x -> x.getStudent() != null).collect(Collectors.toMap(entity::getStudent, x -> x));
//过滤空


long count = list.stream().distinct.count();
boolean isRepeat = count < list.size();
//去重后计数后判断长度,或判断是否有重复值且不需要重复结果


list.stream().collect(Collectors.toMap(x -> Optional.ofNullable(x.getTest()).orElse("null"), y -> y));
//防止空指针


list.stream().collect(Collectors.groupingBy(entity::getName))
//分组


Person result1 = collection.stream()                    // 转化为流
        .filter(x -> "张三".equals(x.getName()))        // 只过滤出"张三"
        .findAny()                                     // 如果找到了就返回
        .orElse(null);                                 // 如果找不到就返回null

list.stream().collect(Collectors.groupingBy(x -> {
                    //按照成员进行分组
                    //key为null 的返回null和对应的list
                    //x表示list 的每个对象
                    if (x != null) {
                        //这里是告诉这个函数通过什么分组
                        return x.getName();
                    } else {
                        //如果根据分组的这个key为空,根据null分组
                        return "未知";
                    }
                }));
//根据要求分组,此处可以把null分为一组


list.stream().collect(Collectors.toMap(People::getName, p -> p, (k, v) -> k));
//Duplicate key  解决key重复问题,如果key重复,则覆盖掉一个


 list.stream().
                collect(Collectors.groupingBy(entity::getMonthNum,
               Collectors.summarizingDouble(entity::getPractical)));
       Map<Integer, Double> resultSet = new HashMap<>();
        collect.forEach((k, v) -> resultSet.put(k, v.getSum()));
// collect.forEach((key,value) -> {
// double sum = value.getSum();这里的是获得sum, 等于上面这句话
//});
//分组后合计,在放进一个map

list.stream().sorted(Comparator.comparing(entity::getMonth).reversed()).collect(Collectors.toList());
//根据月份或者时间降序排序
 list.forEach(item -> {
            String dictValue = item.getDictValue();
            if (egMap.containsKey(dictValue)) {
                CountHighEdDto countHighEdDto = egMap.get(dictValue);
                countHighEdDto.setHighEd(item.getDictLabel());
            } else {
                CountHighEdDto countHighEdDto = new CountHighEdDto(item.getDictLabel(), 0);
                egMap.put(dictValue, countHighEdDto);
                countHighEdDtos.add(countHighEdDto);
            }
        });
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值