Java 常用类库 之 比较接口 Comparator

简介:

http://www.verejava.com/?id=169931036202101

/**
    知识点: 比较类 Comparator

    题目: 将某班学生按数学成绩从小到大排序

    思路:
        1. 抽象出类:
            1.1 班级(ClassSet)
            1.2 学生(Student)
        2. 找出类关系:
            2.1 学生 属于 班级 Student -> ClassSet(多对1)
        3. 找出类属性:
            3.1 ClassSet(班级名称,班级人数)
            3.2 Student(学生名称,数学成绩)
        4. 找出类方法:
            4.1 学生添加到班级 ClassSet{addStudent(Student s)}
            4.2 学生成绩从小到大排序  ClassSet{sortByScore()}
*/
import java.util.Arrays;
import java.util.Comparator;

public class TestComparator {
    
    public static void main(String[] args) {
        //实例化4G班级
        ClassSet c = new ClassSet("4G", 4);
        //添加学生
        c.addStudent(new Student("李明", 90));
        c.addStudent(new Student("李浩", 80));
        c.addStudent(new Student("王涛", 95));
        c.addStudent(new Student("张胜", 70));

        //获得4G班级学生数组集合
        Student[] students = c.getStudents();
        //输出学生信息
        for (Student s : students) {
            if (s != null)
                System.out.println(s.getName() + "," + s.getMathScore());
        }

        System.out.println("\n根据学生成绩升序排序");
        Arrays.sort(students, new StudentAscComparator());
        for (Student s : students) {
            if (s != null)
                System.out.println(s.getName() + "," + s.getMathScore());
        }

        System.out.println("\n根据学生成绩降序排序");
        Arrays.sort(students, new StudentDescComparator());
        for (Student s : students) {
            if (s != null)
                System.out.println(s.getName() + "," + s.getMathScore());
        }

    }
}

class ClassSet {
    private String className;//班级名称
    private int maxSize;//班级学生人数
    private int currentSize;//当前多少学生
    private Student[] students;//所有学生的数组

    public ClassSet(String className, int maxSize) {
        this.className = className;
        this.maxSize = maxSize;
        students = new Student[maxSize];
    }

    public Student[] getStudents() {
        return this.students;
    }

    /**
        添加学生
    */
    public void addStudent(Student s) {
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                students[i] = s;
                currentSize++;
                break;
            }
        }
    }

}

class Student {
    private String name;//学生姓名
    private int mathScore;//数学成绩

    public Student(String name, int mathScore) {
        this.name = name;
        this.mathScore = mathScore;
    }

    public String getName() {
        return this.name;
    }

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

    public int getMathScore() {
        return this.mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }
}

/**
    学生升序排列
*/
class StudentAscComparator implements Comparator {
    public int compare(Object o1, Object o2) {
        if ((o1 instanceof Student) && (o2 instanceof Student)) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            if (s1.getMathScore() > s2.getMathScore())
                return 1;
            if (s1.getMathScore() < s2.getMathScore())
                return -1;
        }
        return 0;
    }
}

/**
    学生降序排列
*/
class StudentDescComparator implements Comparator {
    public int compare(Object o1, Object o2) {
        if ((o1 instanceof Student) && (o2 instanceof Student)) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            if (s1.getMathScore() > s2.getMathScore())
                return -1;
            if (s1.getMathScore() < s2.getMathScore())
                return 1;
        }
        return 0;
    }
}

http://www.verejava.com/?id=169931036202101

目录
相关文章
|
1天前
|
Java 开发者
Java中的Lambda表达式与函数式接口
【6月更文挑战第28天】在现代的Java编程实践中,Lambda表达式和函数式接口已经成为提升代码简洁性和可读性的重要工具。本文将深入探讨Lambda表达式的基本概念、语法结构以及如何与函数式接口结合使用,旨在帮助开发者更好地理解和运用这一特性,以编写出更加优雅和高效的Java代码。
|
4天前
|
Java API 开发者
探索Java中的Lambda表达式和函数式接口
【6月更文挑战第25天】在Java的世界里,Lambda表达式的引入标志着一种全新的编程范式——函数式编程。本文将通过深入解析Lambda表达式及其与函数式接口的结合使用,带领读者领略这一特性如何简化代码,提升开发效率。
|
3天前
|
Java 编译器
论Java中的抽象类与接口
论Java中的抽象类与接口
|
4天前
|
Arthas 监控 Java
Java项目方法调用链路耗时追踪(接口优化)
Java项目方法调用链路耗时追踪(接口优化)
7 0
|
4天前
|
Java 机器人 关系型数据库
Java中的类与接口:抽象与实现的艺术
Java中的类与接口:抽象与实现的艺术
|
6天前
|
Java
java使用Predicate接口中的test对数据进行判断
java使用Predicate接口中的test对数据进行判断
8 0
|
6天前
|
Java
java使用Supplier接口的get生产一个数据
java使用Supplier接口的get生产一个数据
8 0
|
9月前
|
Java
Java接口和抽象类
Java接口和抽象类
61 0
|
1月前
|
设计模式 搜索推荐 Java
java接口和抽象类的区别,以及使用选择
java接口和抽象类的区别,以及使用选择
34 0
|
1月前
|
Java
Java的接口与抽象类的区别
Java的接口与抽象类的区别