Java List集合排序实现方法解析

所属分类: 软件编程 / java 阅读数: 59
收藏 0 赞 0 分享

这篇文章主要介绍了Java List集合排序实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.使用 Collections 工具类中的 sort() 方法

参数不同:

void sort(List list) 在自定义类User里面实现Comparable<User>接口,并重写抽象方法compareTo(Student o);

void sort(List list, Comparator c) 第二个参数为了省事,可以直接使用匿名内部类

public 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 = this.getAge() - o.getAge();//先按照年龄排序 
    if(i == 0){ 
      return this.score - o.getScore();//如果年龄相等了再用分数进行排序 
    } 
    return i; 
  } 
   
} 
 
public static void main(String[] args) { 
    List<User> users = new ArrayList<User>(); 
    users.add(new User(78, 26)); 
    users.add(new User(67, 23)); 
    users.add(new User(34, 56)); 
    users.add(new User(55, 23)); 
    Collections.sort(users); 
    for(User user : users){ 
      System.out.println(user.getScore() + "," + user.getAge()); 
    } 
}
public class Students { 
   
  private int age; 
  private int score; 
   
  public Students(int age, int score){ 
    super(); 
    this.age = age; 
    this.score = score; 
  } 
   
  public int getAge() { 
    return age; 
  } 
  public void setAge(int age) { 
    this.age = age; 
  } 
  public int getScore() { 
    return score; 
  } 
  public void setScore(int score) { 
    this.score = score; 
  } 
} 
public static void main(String[] args) { 
    List<Students> students = new ArrayList<Students>(); 
    students.add(new Students(23, 100)); 
    students.add(new Students(27, 98)); 
    students.add(new Students(29, 99)); 
    students.add(new Students(29, 98)); 
    students.add(new Students(22, 89)); 
    Collections.sort(students, new Comparator<Students>() { 
 
      @Override 
      public int compare(Students o1, Students o2) { 
        int i = o1.getScore() - o2.getScore(); 
        if(i == 0){ 
          return o1.getAge() - o2.getAge(); 
        } 
        return i; 
      } 
    }); 
    for(Students stu : students){ 
      System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); 
    } 
}

2.直接使用list.sort()方法,传入实现Comparator接口的实现类的实例,为了省事直接传入匿名内部类

public class Students {

  private int age;
  private int score;

  public Students(int age, int score){
    this.age = age;
    this.score = score;
  }

  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public int getScore() {
    return score;
  }
  public void setScore(int score) {
    this.score = score;
  }
}
public static void main(String[] args) {
  List<Students> students = new ArrayList<Students>();
  students.add(new Students(23, 100));
  students.add(new Students(27, 98));
  students.add(new Students(29, 99));
  students.add(new Students(29, 98));
  students.add(new Students(22, 89));

  students.sort(new Comparator<Students>() {
    @Override
    public int compare(Students o1, Students o2) {
      int i = o1.getScore() - o2.getScore();
      if (i == 0) {
        return o1.getAge() - o2.getAge();
      }
      return i;
    }
  });

  for (Students stu : students) {
    System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java实现一个简单的文件上传案例示例代码

这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在spring中使用JdbcTemplate操作数据库的几种方式

这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java中的switch case语句使用详解

这篇文章主要介绍了java中的switch case语句使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

深入理解Java 线程池

这篇文章主要介绍了Java 线程池的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解mybatis #{}和${}的区别、传参、基本语法

这篇文章主要介绍了mybatis #{}和${}的区别、传参、基本语法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Mybatis中的 ${} 和 #{}区别与用法

这篇文章主要介绍了Mybatis中的 ${} 和 #{}区别与用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Maven安装过程图文详解

这篇文章主要介绍了Maven安装过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

SpringBoot登录拦截配置详解(实测可用)

这篇文章主要介绍了SpringBoot登录拦截配置详解(实测可用),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 内存溢出案例汇总

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多