List集合进行分组

        在开发过程中会经常遇到把一个List集合中的对象按照某个属性进行分组,然后把分组后的结果再另外处理的这种情况。分组的时候如果是比较简单的只需要分一次组,复杂情况时需要进行二次分组,甚至三次分组。我们可以使用Collectors.groupingBy 来提高工作效率。具体分组请看下面代码。

先创建一个Bean对象。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private String name;
    private Integer age;
    private Date birthday;
    private Double ff;
}

 普通的分组方式,可以通过Map的key是唯一的这种特性进行分组。

public static void main(String[] args) throws CloneNotSupportedException {
        // 需求根据name进行分组,key是name, value是List<Student>的集合
        // 数据初始化
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Student student;
            if (i <2) {
                student = new Student("name", i, new Date(), 11.0);
            } else {
                student = new Student("name2", i, new Date(), 11.0);
            }

            list.add(student);
        }
        // 分组方式1
        Map<String, List<Student>> map = new HashMap<>();
        for (Student student : list) {
            if (map.get(student.getName()) == null) {
                List<Student> studentList = new ArrayList<>();
                studentList.add(student);
                map.put(student.getName(), studentList);
            } else {
                map.get(student.getName()).add(student);
            }
        }
    }

使用collections.groupby来简化代码量

public static void main(String[] args) throws CloneNotSupportedException {
        // 需求根据name进行分组,key是name, value是List<Student>的集合
        // 数据初始化
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Student student;
            if (i <2) {
                student = new Student("name", i, new Date(), 11.0);
            } else {
                student = new Student("name2", i, new Date(), 11.0);
            }

            list.add(student);
        }
   
        // 这句分组与分组方式1的效果相同,但是代码量就少了很多,建议用下面的写法,需要java8以上。
        // 分组方式2
        Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getName));
        
        // 组内再分组,总共是2次分组,适用于先进行第一次分组,然后在第一次分组的基础上再进行一次分组的情况; key: name ,value: Map<ff, List<Student>
        // 先按name进行第一次分组,然后再按ff进行第二次分组;
        Map<String, Map<Double, List<Student>>> collect1 = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getFf)));
        System.out.println(collect1);
        Set<Map.Entry<String, Map<Double, List<Student>>>> entries = collect1.entrySet();
        for (Map.Entry<String, Map<Double, List<Student>>> entry : entries) {
            Map<Double, List<Student>> ffMap = entry.getValue();
            Set<Map.Entry<Double, List<Student>>> entries1 = ffMap.entrySet();
            for (Map.Entry<Double, List<Student>> doubleListEntry : entries1) {
                // 这时候的studentList 出来的 name和ff都是同一组的。
                List<Student> studentList = doubleListEntry.getValue();
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值