List集合排序

直接使用Collections中的方法(主要是单属性)

如对String、Integer类型等非引用类型排序。List<String>集合

升序 Collections.sort(list)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {

List<String> list = new ArrayList<>();
list.add("abc");
list.add("ecd");
list.add("aaa");
list.add("bef");
list.add("cmc");
list.add("cab");
Collections.sort(list); //升序排序
for (String s : list) {
System.out.println(s);
}
}

降序 Collections.reverse(list)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {

List<String> list = new ArrayList<>();
list.add("abc");
list.add("ecd");
list.add("aaa");
list.add("bef");
list.add("cmc");
list.add("cab");
Collections.sort(list); //升序排序
for (String s : list) {
System.out.println(s);
}
Collections.reverse(list); //降序排序
for (String s : list) {
System.out.println(s);
}
}

结果

排序

自定义排序规则(主要用于对象的排序)

如根据用户的年龄排序,对List<User>集合进行排序

如果需要降序,值需要改变规则即可,但是使用的还是Collections.sort()方法

方式一:实现Comparator接口

测试的list中Collections.sort()方法

Collections.sort(userList, new Comparator() {})

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
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
userList.add(new User(19,"hello"));
userList.add(new User(40,"hello2"));
userList.add(new User(20,"hello3"));
userList.add(new User(11,"hello4"));
userList.add(new User(19,"hello5"));
userList.add(new User(30,"hello6"));
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
if (o1.getAge() > o2.getAge()) {
return 1;
}else if (o1.getAge() < o2.getAge()){
return -1;
}else {
return 0;
}
}
});
for (User user : userList) {
System.out.println(user);
}

}

User对象只是JavaBean

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
class User {
int age;
String name;

public User(int age, String name) {
this.age = age;
this.name = name;
}

@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

}

结果:

升序结果

方式二:实现Comparable接口

测试数据正常排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
userList.add(new User(19,"hello"));
userList.add(new User(40,"hello2"));
userList.add(new User(20,"hello3"));
userList.add(new User(11,"hello4"));
userList.add(new User(19,"hello5"));
userList.add(new User(30,"hello6"));
Collections.sort(userList);
for (User user : userList) {
System.out.println(user);
}

}

User对象实现Comparable接口:

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
class User implements Comparable<User>{
int age;
String name;

public User(int age, String name) {
this.age = age;
this.name = name;
}

@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

@Override
public int compareTo(User o) {
if (o.age > age) {
return -1;
} else if (o.age < age) {
return 1;
} else {
return 0;
}
}
}

结果:

升序结果

最后更新: 2020年08月17日 21:53

原始链接: http://ligangit.com/2020/08/17/List集合排序/

× 请我吃糖~
打赏二维码