java操作List对象集合 常用方法

1.对List计数,根据对象某个属性求和,求平均值,求最值等。
2.对List分组,根据对象某个属性分组
3.对Lisr过滤,根据对象某个属性过滤
4.取出一组对象的某个属性组成一个新集合

        List<StudentEntity> list2 = new ArrayList<>();
        StudentEntity studentEntity1 = new StudentEntity();
        studentEntity1.setName("班长");
        studentEntity1.setAge(5);
        list2.add(studentEntity1);

        StudentEntity studentEntity2 = new StudentEntity();
        studentEntity2.setName("副班长");
        studentEntity2.setAge(6);
        list2.add(studentEntity2);

        StudentEntity studentEntity3 = new StudentEntity();
        studentEntity3.setName("学习委员");
        studentEntity3.setAge(7);
        list2.add(studentEntity3);

        StudentEntity studentEntity4 = new StudentEntity();
        studentEntity4.setName("副班长");
        studentEntity4.setAge(5);
        list2.add(studentEntity4);

        //计数
        long count = list2.stream().count();
        System.out.println("计数" + count);

        //最值(最大值:Collector.maxBy,最小值:Collector.minBy)
        Optional<StudentEntity> oldPerson = list2.stream().collect(Collectors.maxBy(Comparator.comparingInt(StudentEntity::getAge)));
        System.out.println("最值" + oldPerson);

        //求和
        int summing = list2.stream().collect(Collectors.summingInt(StudentEntity::getAge));
        System.out.println("求和" + summing);

        //求平均值
        double avg = list2.stream().collect(Collectors.averagingInt(StudentEntity::getAge));
        System.out.println("求平均值" + avg);

        //跟据某个属性分组
        Map<Integer, List<StudentEntity>> collect = list2.stream().collect(Collectors.groupingBy(StudentEntity::getAge));
        System.out.println("跟据某个属性分组" + collect);

        //根据某个属性分组,汇总某个属性
        Map<String, Integer> collect2 = list2.stream().collect(Collectors.groupingBy(StudentEntity::getName,Collectors.summingInt(StudentEntity::getAge)));
        System.out.println("根据某个属性分组,汇总某个属性" +collect2);


        // 取出一组对象的某个属性组成一个新集合
        List<String> names = list2.stream().map(StudentEntity::getName).collect(Collectors.toList());
        System.out.println("names  " + names);

        // 过滤
        List<StudentEntity> names2 = list2.stream().filter(u -> u.getAge() != 5).collect(Collectors.toList());
        System.out.println("过滤 " + names2);

        //根据某个属性添加条件过滤数据
        list2 = list2.stream().filter(u -> u.getName().equals("学习委员")).collect(Collectors.toList());
        System.out.println("根据某个属性添加条件过滤数据 " + list2);
计数4
最值Optional[com.abie.framework.entity.StudentEntity@13969fbe]
求和23
求平均值5.75
跟据某个属性分组{5=[com.abie.framework.entity.StudentEntity@4e04a765, com.abie.framework.entity.StudentEntity@783e6358], 6=[com.abie.framework.entity.StudentEntity@17550481], 7=[com.abie.framework.entity.StudentEntity@13969fbe]}
根据某个属性分组,汇总某个属性{学习委员=7, 副班长=11, 班长=5}
names  [班长, 副班长, 学习委员, 副班长]
过滤 [com.abie.framework.entity.StudentEntity@17550481, com.abie.framework.entity.StudentEntity@13969fbe]
根据某个属性添加条件过滤数据 [com.abie.framework.entity.StudentEntity@13969fbe]

参考自:
https://blog.csdn.net/u010425776/article/details/52346644
https://blog.csdn.net/ctwy291314/article/details/89175611

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值