温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Java怎么多List集合排序

发布时间:2021-05-19 16:16:59 来源:亿速云 阅读:144 作者:Leah 栏目:编程语言

本篇文章为大家展示了使用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的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

上述内容就是使用Java怎么多List集合排序,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI