程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java比較器comparator使用示例分享

java比較器comparator使用示例分享

編輯:JAVA編程入門知識

代碼如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorTest implements Comparator<stuEntity> {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<stuEntity> list = new ArrayList<stuEntity>();
        stuEntity stud1=new stuEntity();
        stud1.setAge(10);
        stud1.setName("abc");
        stuEntity stud2=new stuEntity();
        stud2.setAge(10);
        stud2.setName("bdc");
        stuEntity stud3=new stuEntity();
        stud3.setAge(5);
        stud3.setName("bdd");
        stuEntity stud4=new stuEntity();
        stud4.setAge(30);
        stud4.setName("aad");

        list.add(stud1);
        list.add(stud2);
        list.add(stud3);
        list.add(stud4);

        Collections.sort(list, new ComparatorTest());

        for(stuEntity stud:list){
            System.out.println(stud.getAge()+":"+stud.getName());
        }
    }
/**
 *
 */
    @Override
    public int compare(stuEntity stud1, stuEntity stud2) {
        //根據姓名排序
        int maxname=stud1.getName().compareTo(stud2.getName());
        if(maxname!=0)
            return maxname;
        //根據年齡排序
        int maxage=stud1.getAge()-stud2.getAge();
        //if(maxage!=0)
            return maxage;
    }
}

輸出:
代碼如下:

30:aad
10:abc
10:bdc
5:bdd

java的比較器很有用,實現Comparator接口的compare()這個回調方法來制定排序規則,然後調用Collections.sort(list, new ComparatorTest());就可以將List進行排序,很方便

使用時要注意compare()方法中的return的先後順序,優先的排序規則要寫在前面

實體類:

代碼如下:

/**
 * 學生實體類
 *
 */
public class stuEntity {
    private int studentId;// 學號
    private String name;
    private int age;
    private String sex;// 性別
    private int roomNumber;// 房間號
    private String degree;//學位
    private int grade;//年級
    private String deviceNumber;// 設備號
    private int groupNumber;// 所屬的小組
    private int javaScore;// java成績
    private int netScore;// NET成績
    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    /**
     * 初始化有參構造函數
     *
     * @param id
     * @param name
     * @param age
     * @param sex
     * @param roomNumber
     * @param deviceNumber
     * @param groupNumber
     * @param javaScore
     * @param netScore
     */
    public stuEntity(String name, int age, String sex, int roomNumber,
            String deviceNumber, int groupNumber, int javaScore, int netScore) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.roomNumber = roomNumber;
        this.deviceNumber = deviceNumber;
        this.groupNumber = groupNumber;
        this.javaScore = javaScore;
        this.netScore = netScore;
    }

    /**
     * 無參構造函數
     */
    public stuEntity() {

    }

    public int getJavaScore() {
        return javaScore;
    }

    public void setJavaScore(int javaScore) {
        this.javaScore = javaScore;
    }

    public int getNetScore() {
        return netScore;
    }

    public void setNetScore(int netScore) {
        this.netScore = netScore;
    }

   

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getRoomNumber() {
        return roomNumber;
    }

    public void setRoomNumber(int roomNumber) {
        this.roomNumber = roomNumber;
    }

    public String getDeviceNumber() {
        return deviceNumber;
    }

    public void setDeviceNumber(String deviceNumber) {
        this.deviceNumber = deviceNumber;
    }

    public int getGroupNumber() {
        return groupNumber;
    }

    public void setGroupNumber(int groupNumber) {
        this.groupNumber = groupNumber;
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved