Java8-GroupBy实现集合的分组

Java-Sort、Stream-List两种排序方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Subject subject1 = new Subject("语文", 91.0);
Subject subject2 = new Subject("语文", 94.0);
Subject subject3 = new Subject("语文", 92.0);
Subject subject4 = new Subject("语文", 90.0);
List<Subject> subjectList = new ArrayList<>();
subjectList.add(subject1);
subjectList.add(subject2);
subjectList.add(subject3);
subjectList.add(subject4);
List<Subject> subjectListNew = new ArrayList<>();

//排序生效,使用stream和collect
subjectListNew = subjectList.stream().sorted((s1, s2) -> s1.getGrade().compareTo(s2.getGrade())).collect(Collectors.toList());
System.out.println(JSON.toJSONString(subjectListNew));
//[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]
System.out.println(JSON.toJSONString(subjectList));//原集合没变
//[{"grade":91,"id":1,"name":"语文"},{"grade":94,"id":2,"name":"数学"},{"grade":92,"id":3,"name":"英语"},{"grade":90,"id":4,"name":"语文"}]


//排序生效,不使用stream和collect
subjectList.sort((s1, s2) -> s1.getGrade().compareTo(s2.getGrade()));
System.out.println(JSON.toJSONString(subjectList));//原集合变化
//[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]


//排序不生效,使用stream时需要在末尾collect
subjectList.stream().sorted((s1, s2) -> s1.getGrade().compareTo(s2.getGrade()));
System.out.println(JSON.toJSONString(subjectList));
//[{"grade":91.0,"name":"语文"},{"grade":94.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":90.0,"name":"语文"}]


//分数升序排列
subjectListNew = subjectList.stream().sorted((s1, s2) -> {
return s1.getGrade().compareTo(s2.getGrade());
}).collect(Collectors.toList());//如果使用分号,需要加{}和return
System.out.println(JSON.toJSONString(subjectListNew));
//[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]


//分数降序排列
subjectListNew = subjectList.stream().sorted(Comparator.comparingDouble(Subject::getGrade).reversed()).collect(Collectors.toList());
System.out.println(JSON.toJSONString(subjectListNew));
//[{"grade":94.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":90.0,"name":"语文"}]

//分数升序排列
subjectListNew = subjectList.stream().sorted(Comparator.comparingDouble(Subject::getGrade)).collect(Collectors.toList());
System.out.println(JSON.toJSONString(subjectListNew));
//[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Subject {

String name;
Double grade;

public Subject(String name, Double grade) {
this.name = name;
this.grade = grade;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getGrade() {
return grade;
}

public void setGrade(Double grade) {
this.grade = grade;
}
}

本文标题:Java8-GroupBy实现集合的分组

文章作者:郑上进

本文链接:http://blog.zjc123.cn/2019/04/05/Java8-Sort、Stream-ListTwoSortingMethods/

许可协议:转载请保留原文链接及作者姓名。

-------------本文结束感谢您的阅读-------------