Java8对集合的处理(1)

一些前置条件

 List<Student> list = new ArrayList<>();
        Student stu1 = new Student();
        stu1.setId(1001L);
        stu1.setCode("1001");
        stu1.setName("小明");
        stu1.setLevel(1);
        stu1.setSex(1);
        stu1.setBonus(BigDecimal.valueOf(10.5));
        Student stu2 = new Student();
        stu2.setId(1002L);
        stu2.setCode("1002");
        stu2.setName("张三");
        stu2.setLevel(1);
        stu2.setSex(1);
        stu2.setBonus(BigDecimal.valueOf(10));
        Student stu3 = new Student();
        stu3.setId(1003L);
        stu3.setCode("1003");
        stu3.setName("王五");
        stu3.setLevel(2);
        stu3.setSex(0);
        stu3.setBonus(BigDecimal.valueOf(20));
        Student stu4 = new Student();
        stu4.setId(1004L);
        stu4.setCode("1004");
        stu4.setName("Kevin");
        stu4.setLevel(3);
        stu4.setSex(0);
        stu4.setBonus(BigDecimal.valueOf(100));
        Student stu5 = new Student();
        stu5.setId(1005L);
        stu5.setCode("1005");
        stu5.setLevel(3);
        stu5.setSex(0);
        stu5.setName("月亮");
        stu5.setBonus(BigDecimal.valueOf(1.5));
        list.add(stu3);
        list.add(stu1);
        list.add(stu2);
        list.add(stu4);
        list.add(stu5);

场景1

过滤性别的是女的:即剔除sex=0的student,留下sex=1的student。条件满足的留下

List<Student> listResult = list.stream().filter(s -> s.getSex() == 1).collect(Collectors.toList());

场景2

根据年纪(level)进行分组。key为年级,List为同年级的集合。

Map<Integer, List<Student>> studentMap = list.stream().collect(Collectors.groupingBy(Student::getLevel));

场景3

求和,将所有student的bonus求和;不能过滤BigDecimal为null的情况

BigDecimal total = list.stream().map(Student::getBonus).reduce(BigDecimal.ZERO, BigDecimal::add);

场景4

排序 单字段根据id排序 升序

list.sort(Comparator.comparing(Student::getId));

场景5

排序,单字段根据id排序,降序

list.sort(Comparator.comparing(Student::getId).reversed());

场景6

多字段进行排序

list.sort(Comparator.comparing(Student::getId).reversed().thenComparing(Student::getBonus));

场景7

去重复

List<Integer> targetLis = sourceList.stream().distinct().collect(Collectors.toList());

场景8

获取对象的某个字段重新组装成List

List<Long> studentId = list.stream().map(s -> s.getId()).collect(Collectors.toList());```

场景9

求两个list的交集

List<String> intersection = list1.stream().filter(item->list2.contains(item)).collect(Collectors.toList());

场景10

差集 list1-list2

List<String> reduce =list1.stream().filter(item->!list2.contains(item)).collect(Collectors.toList());

场景11

使用apache common collection对集合进行分割

List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subs = ListUtils.partition(intList, 3);

场景12

使用google guava对List进行分割

//使用guava对list进行分割
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
//按每3个一组分割
List<List<Integer>> parts = Lists.partition(intList , 50);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值