Java中如何将对象按属性进行分组呢?
下文笔者讲述java中对象按属性分组的方法分享,如下所示
实现思路: 借助java8中的stream流,可对集合进行分组操作例:
//集合定义 list<Student> studlist = new ArrayList<>(); studlist.add(new Student("1", "aa", "China")); studlist.add(new Student("2", "bb", "China")); studlist.add(new Student("3", "cc", "China")); studlist.add(new Student("4", "dd", "New York")); studlist.add(new Student("5", "ee", "California")); studlist.add(new Student("6", "ff", "New York")); //group by Map<String, List<Student>> list = studlist.stream().collect(Collectors.groupingBy(w -> w.stud_location));
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。