当前位置:   article > 正文

Java - List排序_java list排序

java list排序

List排序方法

主要有三种方法(按推荐度排序):

  1. JDK8的stream
  2. Comparator#compare()
  3. Comparable#compareTo()

法1:list的sort()

  1. package com.example.a;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. class User{
  6. private Integer score;
  7. private Integer age;
  8. public User(Integer score, Integer age){
  9. super();
  10. this.score = score;
  11. this.age = age;
  12. }
  13. public Integer getScore() {
  14. return score;
  15. }
  16. public void setScore(Integer score) {
  17. this.score = score;
  18. }
  19. public Integer getAge() {
  20. return age;
  21. }
  22. public void setAge(Integer age) {
  23. this.age = age;
  24. }
  25. }
  26. public class Demo {
  27. public static void main(String[] args) {
  28. List<User> users = new ArrayList<>();
  29. users.add(new User(95, 26));
  30. users.add(new User(84, 23));
  31. users.add(new User(96, 25));
  32. users.add(new User(95, 24));
  33. // 单字段排序
  34. users.sort(Comparator.comparing(User::getAge));
  35. for(User user : users){
  36. System.out.println(user.getScore() + "," + user.getAge());
  37. }
  38. System.out.println("---------------------------------");
  39. // 多字段排序(法1)
  40. users.sort((o1, o2) -> {
  41. // 这里必须要在中间加分隔符。否则,若都是数字,会变成数字相加,再转为字符串
  42. String tmp1 = o1.getScore() + ":" + o1.getAge();
  43. String tmp2 = o2.getScore() + ":" + o2.getAge();
  44. return tmp1.compareTo(tmp2);
  45. });
  46. for(User user : users){
  47. System.out.println(user.getScore() + ":" + user.getAge());
  48. }
  49. System.out.println("---------------------------------");
  50. // 多字段排序(法2)
  51. users.sort((o1, o2) -> {
  52. int i = o2.getScore() - o1.getScore();
  53. if (i == 0) {
  54. return o1.getAge() - o2.getAge();
  55. }
  56. return i;
  57. });
  58. for(User user : users){
  59. System.out.println(user.getScore() + "," + user.getAge());
  60. }
  61. }
  62. }
  63. //测试输出
  64. 84,23
  65. 95,24
  66. 96,25
  67. 95,26
  68. ---------------------------------
  69. 84:23
  70. 95:24
  71. 95:26
  72. 96:25
  73. ---------------------------------
  74. 96,25
  75. 95,24
  76. 95,26
  77. 84,23

法2:JDK8的stream

方法说明
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()) // 倒序排序(从大到小)

返回排序后的流

  1. //4、sorted:排序,根据名字倒序
  2. userList.stream().sorted(Comparator.comparing(User::getName).reversed()).collect(Collectors.toList()).forEach(System.out::println);

原始类型排序

  1. List<String> list = Arrays.asList("aa", "ff", "dd");
  2. //String 类自身已实现Comparable接口
  3. list.stream().sorted().forEach(System.out::println);
  4.  
  5. //结果:
  6. aa
  7. dd
  8. ff

对象单字段排序

  1. User u1 = new User("dd", 40);
  2. User u2 = new User("bb", 20);
  3. User u3 = new User("aa", 20);
  4. User u4 = new User("aa", 30);
  5. List<User> userList = Arrays.asList(u1, u2, u3, u4);
  6.  
  7. //按年龄升序
  8. userList.stream().sorted(Comparator.comparing(User::getAge))
  9.         .forEach(System.out::println);
  10.  
  11. //结果
  12. User(name=bb, age=20)
  13. User(name=aa, age=20)
  14. User(name=aa, age=30)
  15. User(name=dd, age=40)

对象多字段、全部升序排序 

  1. //先按年龄升序,年龄相同则按姓名升序
  2. User u1 = new User("dd", 40);
  3. User u2 = new User("bb", 20);
  4. User u3 = new User("aa", 20);
  5. User u4 = new User("aa", 30);
  6. List<User> userList = Arrays.asList(u1, u2, u3, u4);
  7.  
  8. // 写法1(推荐)
  9. userList.stream().sorted(Comparator
  10.                 .comparing(User::getAge)
  11.                 .thenComparing(User::getName)
  12.         // 可以写多个.thenComparing
  13. ).forEach(System.out::println);
  14.  
  15. System.out.println("------------------------------------");
  16.  
  17. // 写法2
  18. userList.stream().sorted(
  19.         (o1, o2) -> {
  20.             String tmp1 = o1.getAge() + o1.getName();
  21.             String tmp2 = o2.getAge() + o2.getName();
  22.             return tmp1.compareTo(tmp2);
  23.         }
  24. ).forEach(System.out::println);
  25.  
  26. System.out.println("------------------------------------");
  27.  
  28. // 写法3
  29. userList.stream().sorted(
  30.         (o1, o2) -> {
  31.             if (!o1.getAge().equals(o2.getAge())) {
  32.                 return o1.getAge().compareTo(o2.getAge());
  33.             } else {
  34.                 return o1.getName().compareTo(o2.getName());
  35.             }
  36.         }
  37. ).forEach(System.out::println);
  38.  
  39. //结果
  40. User(name=aa, age=20)
  41. User(name=bb, age=20)
  42. User(name=aa, age=30)
  43. User(name=dd, age=40)
  44. ------------------------------------
  45. User(name=aa, age=20)
  46. User(name=bb, age=20)
  47. User(name=aa, age=30)
  48. User(name=dd, age=40)
  49. ------------------------------------
  50. User(name=aa, age=20)
  51. User(name=bb, age=20)
  52. User(name=aa, age=30)
  53. User(name=dd, age=40)

对象多字段、升序+降序

  1. //先按年龄升序,年龄相同则按姓名降序
  2. User u1 = new User("dd", 40);
  3. User u2 = new User("bb", 20);
  4. User u3 = new User("aa", 20);
  5. User u4 = new User("aa", 30);
  6. List<User> userList = Arrays.asList(u1, u2, u3, u4);
  7.  
  8. userList.stream().sorted(
  9.         (o1, o2) -> {
  10.             if (!o1.getAge().equals(o2.getAge())) {
  11.                 return o1.getAge().compareTo(o2.getAge());
  12.             } else {
  13.                 return o2.getName().compareTo(o1.getName());
  14.             }
  15.         }
  16. ).forEach(System.out::println);
  17.  
  18. //结果
  19. User(name=bb, age=20)
  20. User(name=aa, age=20)
  21. User(name=aa, age=30)
  22. User(name=dd, age=40)

 

法3:Comparator#compare()

需求:用户有成绩和年龄。按成绩排序,若成绩相同,则按年龄排序。

  1. package org.example.a;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. class User{
  7. private int score;
  8. private int age;
  9. public User(int score, int age){
  10. super();
  11. this.score = score;
  12. this.age = age;
  13. }
  14. public int getScore() {
  15. return score;
  16. }
  17. public void setScore(int score) {
  18. this.score = score;
  19. }
  20. public int getAge() {
  21. return age;
  22. }
  23. public void setAge(int age) {
  24. this.age = age;
  25. }
  26. }
  27. public class Demo {
  28. public static void main(String[] args) {
  29. List<User> users = new ArrayList<User>();
  30. users.add(new User(95, 26));
  31. users.add(new User(84, 23));
  32. users.add(new User(96, 25));
  33. users.add(new User(95, 24));
  34. Collections.sort(users, new Comparator<User>() {
  35. @Override
  36. public int compare(User o1, User o2) {
  37. int i = o2.getScore() - o1.getScore();
  38. if(i == 0){
  39. return o1.getAge() - o2.getAge();
  40. }
  41. return i;
  42. }
  43. });
  44. for(User user : users){
  45. System.out.println(user.getScore() + "," + user.getAge());
  46. }
  47. }
  48. }
  49. //执行结果
  50. 96,25
  51. 95,24
  52. 95,26
  53. 84,23

法4:Comparable#compareTo()

默认按增序排序:

需求:用户有成绩和年龄。按成绩降序排序,若成绩相同,则按年龄正序排序。

  1. package org.example.a;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. class User implements Comparable<User>{
  6. private int score;
  7. private int age;
  8. public User(int score, int age){
  9. super();
  10. this.score = score;
  11. this.age = age;
  12. }
  13. public int getScore() {
  14. return score;
  15. }
  16. public void setScore(int score) {
  17. this.score = score;
  18. }
  19. public int getAge() {
  20. return age;
  21. }
  22. public void setAge(int age) {
  23. this.age = age;
  24. }
  25. @Override
  26. public int compareTo(User o) {
  27. int i = o.getScore() - this.getScore();
  28. if(i == 0){
  29. return this.getAge() - o.getAge();
  30. }
  31. return i;
  32. }
  33. }
  34. public class Demo {
  35. public static void main(String[] args) {
  36. List<User> users = new ArrayList<User>();
  37. users.add(new User(95, 26));
  38. users.add(new User(84, 23));
  39. users.add(new User(96, 25));
  40. users.add(new User(95, 24));
  41. Collections.sort(users);
  42. for(User user : users){
  43. System.out.println(user.getScore() + "," + user.getAge());
  44. }
  45. }
  46. }
  47. //执行结果
  48. 96,25
  49. 95,24
  50. 95,26
  51. 84,23

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/784126
推荐阅读
相关标签
  

闽ICP备14008679号