实体类

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
49
50
51
52
53
54
55
56
57
package com.ledao.entity;

/**
* 学生实体类
*
* @author LeDao
* @company
* @create 2021-11-21 23:19
*/
public class Student {

/**
* 编号
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;

public Student() {
}

public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

排序

Collections.sort

该方法有两个参数,参数一为要排序的对象集合,参数二为比较器

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
import com.ledao.entity.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* @author LeDao
* @company
* @create 2021-11-22 20:43
*/
public class Test {

public static void main(String[] args) {

List<Student> studentList = new ArrayList<>(16);
studentList.add(new Student(2, "atn", 14));
studentList.add(new Student(1, "tom", 12));
studentList.add(new Student(3, "bob", 11));
studentList.add(new Student(5, "alex", 13));
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
for (Student student : studentList) {
System.out.println(student.getId()+","+student.getName()+","+student.getAge());
}
}
}

List.sort

方法参数为比较器

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
import com.ledao.entity.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* @author LeDao
* @company
* @create 2021-11-22 20:43
*/
public class Test {

public static void main(String[] args) {

List<Student> studentList = new ArrayList<>(16);
studentList.add(new Student(2, "atn", 14));
studentList.add(new Student(1, "tom", 12));
studentList.add(new Student(3, "bob", 11));
studentList.add(new Student(5, "alex", 13));
studentList.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
for (Student student : studentList) {
System.out.println(student.getId()+","+student.getName()+","+student.getAge());
}
}
}

Stream流操作

通过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
import com.ledao.entity.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author LeDao
* @company
* @create 2021-11-22 20:43
*/
public class Test {

public static void main(String[] args) {

List<Student> studentList = new ArrayList<>(16);
studentList.add(new Student(2, "atn", 14));
studentList.add(new Student(1, "tom", 12));
studentList.add(new Student(3, "bob", 11));
studentList.add(new Student(5, "alex", 13));
List<Student> resultList=studentList.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId()-o2.getId();
}
}).collect(Collectors.toList());
for (Student student : resultList) {
System.out.println(student.getId()+","+student.getName()+","+student.getAge());
}
}
}

实体类实现Comparable接口

实体类实现Comparable接口的compareTo方法,然后再使用Collections.sort方法(参数只有一个:要排序的集合)

实体类

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ledao.entity;

/**
* 学生实体类
*
* @author LeDao
* @company
* @create 2021-11-21 23:19
*/
public class Student implements Comparable<Student>{

/**
* 编号
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;

public Student() {
}

public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public int compareTo(Student o) {
return o.getId()-this.getId();
}
}

测试方法

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
import com.ledao.entity.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author LeDao
* @company
* @create 2021-11-22 20:43
*/
public class Test {

public static void main(String[] args) {

List<Student> studentList = new ArrayList<>(16);
studentList.add(new Student(2, "atn", 14));
studentList.add(new Student(1, "tom", 12));
studentList.add(new Student(3, "bob", 11));
studentList.add(new Student(5, "alex", 13));
Collections.sort(studentList);
for (Student student : studentList) {
System.out.println(student.getId()+","+student.getName()+","+student.getAge());
}
}
}