Lambda使用

  // 查询所有栏目集合
List<ColumnEntity> columns = columnApiService.queryAllColumns();

 // 过滤type为4的数据
List<ColumnEntity> list = columns.stream().filter(p -> p.getType() == 4).collect(Collectors.toList());

// 根据Sort字段排序(正序)
List<ColumnEntity> sortList = parentList.stream().sorted(Comparator.comparing(ColumnEntity::getSort)).collect(Collectors.toList());
// 根据Sort字段排序(倒序)
List<ColumnEntity> sortList = parentList.stream().sorted(Comparator.comparing(ColumnEntity::getSort).reversed()).collect(Collectors.toList());
//ParentId>0且ParentId=Id
 List<ColumnEntity> collect2 = list.stream().filter(item -> item.getParentId() > 0 && item.getId().equals(parentList.get(0))).collect(Collectors.toList());

//转换
List<Long> userIds = strList.stream().map(s -> Long.parseLong(s == null ? "-1000" : s.trim())).collect(Collectors.toList());

//转成set
Set<Integer> ageSet = list.stream().map(Student::getAge).collect(Collectors.toSet()); // [20, 10]
 

//转成map key不能相同 否则报错
Map<String, Integer> studentMap = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge)); // {cc=10, bb=20, aa=10}

//字符串分隔符连接
String joinName = list.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")")); // (aa,bb,cc)

//查询总数
Long count = list.stream().collect(Collectors.counting()); 

//最大年龄 (最小的minBy同理)
Integer maxAge = list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get(); /

//所有人的年龄
Integer sumAge = list.stream().collect(Collectors.summingInt(Student::getAge)); 

//平均年龄
Double averageAge = list.stream().collect(Collectors.averagingDouble(Student::getAge)); 

//带上以上所有方法
DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Student::getAge));
System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());

//分组
Map<Integer, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAge));

//多重分组,先根据类型分再根据年龄分
Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));

//分区
//分成两部分,一部分大于10岁,一部分小于等于10岁
Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));


       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值