java8 lambda表达式 groupingBy 分组操作

Posted on 2019-06-29 16:48  豆豆2018  阅读(46607)  评论(0编辑  收藏  举报

实验实体类 Student

 

 1 package utill;
 2 
 3 /**
 4  * @author YangTao
 5  * @date 2019/6/17 21:06
 6  */
 7 public class Student {
 8     int age;
 9     String username;
10     String code;
11 
12     public Student() {
13     }
14 
15     public Student(int age, String username, String code) {
16         this.age = age;
17         this.username = username;
18         this.code = code;
19     }
20 
21     public int getAge() {
22         return age;
23     }
24 
25     public void setAge(int age) {
26         this.age = age;
27     }
28 
29     public String getUsername() {
30         return username;
31     }
32 
33     public void setUsername(String username) {
34         this.username = username;
35     }
36 
37     public String getCode() {
38         return code;
39     }
40 
41     public void setCode(String code) {
42         this.code = code;
43     }
44 
45     @Override
46     public String toString() {
47         return "Student{" + "age=" + age + ", username='" + username + '\'' + ", code='" + code + '\'' + '}';
48     }
49 }

先构造一个list<Student> 集合,分组的原理是把这个集合根据我们输入的条件划分为一个个小的集合,将大集合拆分为小集合

两种情况,1.单一条件进行集合划分,比如我根据我的名字,或者性别,将集合划分

2.复合条件,两个或者多个条件,需要自己构造一个合并条件的方法,返回值为String 

 1 import utill.Student;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Map;
 6 import java.util.stream.Collectors;
 7 
 8 /**
 9  * @author YangTao
10  * @date 2019/6/29 16:23
11  */
12 public class GroupByDemo {
13     public static void main(String[] args) {
14         Student student1 = new Student(200, "yangtao", "1");
15         Student student2 = new Student(220, "yangtao", "1");
16         Student student3 = new Student(500, "yangtao", "2");
17         Student student4 = new Student(504, "yangtao", "2");
18         List<Student> studentList =new ArrayList<>();
19         studentList.add(student1);
20         studentList.add(student2);
21         studentList.add(student3);
22         studentList.add(student4);
23 
24         //单一分组条件,根据code
25         Map<String, List<Student>> singleMap = studentList.stream().collect(Collectors.groupingBy(Student::getCode));
26         //{1=[Student{age=200, username='yangtao', code='1'}, Student{age=220, username='yangtao', code='1'}],
27         // 2=[Student{age=500, username='yangtao', code='2'}, Student{age=504, username='yangtao', code='2'}]}
28 
29         //组合分组条件
30 
31         Map<String, List<Student>> complexMap = studentList.stream().collect(Collectors.groupingBy(e -> fetchGroupKey(e)));
32         List<Student> studentList1 = complexMap.get("yangtao+1");
33         List<Student> studentList2 = complexMap.get("yangtao+2");
34         System.out.println(complexMap);
35 //       {yangtao+2=[Student{age=500, username='yangtao', code='2'}, Student{age=504, username='yangtao', code='2'}], yangtao+1=[Student{age=200, username='yangtao', code='1'}, Student{age=220, username='yangtao', code='1'}]}
36 
37         System.out.println(studentList1);
38         System.out.println(studentList2);
39         //[Student{age=200, username='yangtao', code='1'}, Student{age=220, username='yangtao', code='1'}]
40         //[Student{age=500, username='yangtao', code='2'}, Student{age=504, username='yangtao', code='2'}]
41 
42     }
43     private static String fetchGroupKey(Student student){
44         return student.getUsername() +"+"+ student.getCode();
45     }
46 
47 }