欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

Java

當(dāng)前位置:主頁(yè) > 軟件編程 > Java >

Java List集合排序?qū)崿F(xiàn)方法解析

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊: 次

這篇文章主要介紹了Java List集合排序?qū)崿F(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

參數(shù)不同:

void sort(List list) 在自定義類User里面實(shí)現(xiàn)Comparable<User>接口,并重寫(xiě)抽象方法compareTo(Student o);

void sort(List list, Comparator c) 第二個(gè)參數(shù)為了省事,可以直接使用匿名內(nèi)部類

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();//如果年齡相等了再用分?jǐn)?shù)進(jìn)行排序 
    } 
    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()方法,傳入實(shí)現(xiàn)Comparator接口的實(shí)現(xiàn)類的實(shí)例,為了省事直接傳入匿名內(nèi)部類

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());
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Java線程狀態(tài)運(yùn)行原理解析

欄    目:Java

下一篇:Spring實(shí)戰(zhàn)之@Autowire注解用法詳解

本文標(biāo)題:Java List集合排序?qū)崿F(xiàn)方法解析

本文地址:http://mengdiqiu.com.cn/a1/Java/8837.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有