赞
踩
List排序方法
主要有三种方法(按推荐度排序):
- JDK8的stream
- Comparator#compare()
- Comparable#compareTo()
- package com.example.a;
-
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
-
-
- class User{
-
- private Integer score;
-
- private Integer age;
-
- public User(Integer score, Integer age){
- super();
- this.score = score;
- this.age = age;
- }
-
- public Integer getScore() {
- return score;
- }
-
- public void setScore(Integer score) {
- this.score = score;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- }
-
-
- public class Demo {
- public static void main(String[] args) {
- List<User> users = new ArrayList<>();
- users.add(new User(95, 26));
- users.add(new User(84, 23));
- users.add(new User(96, 25));
- users.add(new User(95, 24));
-
- // 单字段排序
- users.sort(Comparator.comparing(User::getAge));
- for(User user : users){
- System.out.println(user.getScore() + "," + user.getAge());
- }
-
- System.out.println("---------------------------------");
-
- // 多字段排序(法1)
- users.sort((o1, o2) -> {
- // 这里必须要在中间加分隔符。否则,若都是数字,会变成数字相加,再转为字符串
- String tmp1 = o1.getScore() + ":" + o1.getAge();
- String tmp2 = o2.getScore() + ":" + o2.getAge();
- return tmp1.compareTo(tmp2);
- });
- for(User user : users){
- System.out.println(user.getScore() + ":" + user.getAge());
- }
-
- System.out.println("---------------------------------");
-
- // 多字段排序(法2)
- users.sort((o1, o2) -> {
- int i = o2.getScore() - o1.getScore();
- if (i == 0) {
- return o1.getAge() - o2.getAge();
- }
- return i;
- });
- for(User user : users){
- System.out.println(user.getScore() + "," + user.getAge());
- }
- }
-
- }
-
- //测试输出
- 84,23
- 95,24
- 96,25
- 95,26
- ---------------------------------
- 84:23
- 95:24
- 95:26
- 96:25
- ---------------------------------
- 96,25
- 95,24
- 95,26
- 84,23
方法 | 说明 |
sorted() | 自然排序(从小到大),流中元素需实现Comparable接口。 例:list.stream().sorted() |
sorted(Comparator com) | 定制排序。常用以下几种: list.stream().sorted(Comparator.reverseOrder()) //倒序排序(从大到小) list.stream().sorted(Comparator.comparing(Student::getAge)) //顺序排序(从小到大) list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) // 倒序排序(从大到小) |
返回排序后的流
- //4、sorted:排序,根据名字倒序
- userList.stream().sorted(Comparator.comparing(User::getName).reversed()).collect(Collectors.toList()).forEach(System.out::println);
- List<String> list = Arrays.asList("aa", "ff", "dd");
- //String 类自身已实现Comparable接口
- list.stream().sorted().forEach(System.out::println);
-
- //结果:
- aa
- dd
- ff
- User u1 = new User("dd", 40);
- User u2 = new User("bb", 20);
- User u3 = new User("aa", 20);
- User u4 = new User("aa", 30);
- List<User> userList = Arrays.asList(u1, u2, u3, u4);
-
- //按年龄升序
- userList.stream().sorted(Comparator.comparing(User::getAge))
- .forEach(System.out::println);
-
- //结果
- User(name=bb, age=20)
- User(name=aa, age=20)
- User(name=aa, age=30)
- User(name=dd, age=40)
- //先按年龄升序,年龄相同则按姓名升序
- User u1 = new User("dd", 40);
- User u2 = new User("bb", 20);
- User u3 = new User("aa", 20);
- User u4 = new User("aa", 30);
- List<User> userList = Arrays.asList(u1, u2, u3, u4);
-
- // 写法1(推荐)
- userList.stream().sorted(Comparator
- .comparing(User::getAge)
- .thenComparing(User::getName)
- // 可以写多个.thenComparing
- ).forEach(System.out::println);
-
- System.out.println("------------------------------------");
-
- // 写法2
- userList.stream().sorted(
- (o1, o2) -> {
- String tmp1 = o1.getAge() + o1.getName();
- String tmp2 = o2.getAge() + o2.getName();
- return tmp1.compareTo(tmp2);
- }
- ).forEach(System.out::println);
-
- System.out.println("------------------------------------");
-
- // 写法3
- userList.stream().sorted(
- (o1, o2) -> {
- if (!o1.getAge().equals(o2.getAge())) {
- return o1.getAge().compareTo(o2.getAge());
- } else {
- return o1.getName().compareTo(o2.getName());
- }
- }
- ).forEach(System.out::println);
-
- //结果
- User(name=aa, age=20)
- User(name=bb, age=20)
- User(name=aa, age=30)
- User(name=dd, age=40)
- ------------------------------------
- User(name=aa, age=20)
- User(name=bb, age=20)
- User(name=aa, age=30)
- User(name=dd, age=40)
- ------------------------------------
- User(name=aa, age=20)
- User(name=bb, age=20)
- User(name=aa, age=30)
- User(name=dd, age=40)
- //先按年龄升序,年龄相同则按姓名降序
- User u1 = new User("dd", 40);
- User u2 = new User("bb", 20);
- User u3 = new User("aa", 20);
- User u4 = new User("aa", 30);
- List<User> userList = Arrays.asList(u1, u2, u3, u4);
-
- userList.stream().sorted(
- (o1, o2) -> {
- if (!o1.getAge().equals(o2.getAge())) {
- return o1.getAge().compareTo(o2.getAge());
- } else {
- return o2.getName().compareTo(o1.getName());
- }
- }
- ).forEach(System.out::println);
-
- //结果
- User(name=bb, age=20)
- User(name=aa, age=20)
- User(name=aa, age=30)
- User(name=dd, age=40)
需求:用户有成绩和年龄。按成绩排序,若成绩相同,则按年龄排序。
- package org.example.a;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
-
-
- class User{
-
- private int score;
-
- private int age;
-
- public User(int score, int age){
- super();
- this.score = score;
- this.age = age;
- }
-
- public int getScore() {
- return score;
- }
-
- public void setScore(int score) {
- this.score = score;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- }
-
-
- public class Demo {
- public static void main(String[] args) {
- List<User> users = new ArrayList<User>();
- users.add(new User(95, 26));
- users.add(new User(84, 23));
- users.add(new User(96, 25));
- users.add(new User(95, 24));
- Collections.sort(users, new Comparator<User>() {
- @Override
- public int compare(User o1, User o2) {
- int i = o2.getScore() - o1.getScore();
- if(i == 0){
- return o1.getAge() - o2.getAge();
- }
- return i;
- }
- });
- for(User user : users){
- System.out.println(user.getScore() + "," + user.getAge());
- }
- }
-
- }
-
- //执行结果
- 96,25
- 95,24
- 95,26
- 84,23
默认按增序排序:
需求:用户有成绩和年龄。按成绩降序排序,若成绩相同,则按年龄正序排序。
- package org.example.a;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
-
- class User implements Comparable<User>{
-
- private int score;
-
- private int age;
-
- public User(int score, int age){
- super();
- this.score = score;
- this.age = age;
- }
-
- public int getScore() {
- return score;
- }
-
- public void setScore(int score) {
- this.score = score;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- @Override
- public int compareTo(User o) {
- int i = o.getScore() - this.getScore();
- if(i == 0){
- return this.getAge() - o.getAge();
- }
- return i;
- }
- }
-
-
- public class Demo {
- public static void main(String[] args) {
- List<User> users = new ArrayList<User>();
- users.add(new User(95, 26));
- users.add(new User(84, 23));
- users.add(new User(96, 25));
- users.add(new User(95, 24));
- Collections.sort(users);
- for(User user : users){
- System.out.println(user.getScore() + "," + user.getAge());
- }
- }
-
- }
-
- //执行结果
- 96,25
- 95,24
- 95,26
- 84,23
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。