PTA_7-3 职工排序题详解


前言

学习一下题目中所涉及到的comparable和comparator的知识点和两者的区别
如果需要答案直接到最后复制


一、题目

1. 为某保险公司设计一个职工管理系统,其中职工类的属性有:职工编号,姓名,性别,团体险业绩,个体险业绩;方法有:
每个属性对应的set,get方法;
不带参数的构造方法;
带参数的构造方法,完成对职工属性的初始化;
该类实现接口Comparable,完成对职工总业绩的比较。

2. 设计一个类,实现Comparator接口,完成对团体险业绩的比较;

3. 在Main类中,创建一个职工的线性表,分别完成对职工线性表按照总业绩升序排序,按照团体险业绩升序排序。
注意:不要设计键盘输入职工信息,可根据样例中提供的数据直接创建职工对象;

输入格式:
输出格式:
各项之间用逗号“,”分隔

输入样例:
在这里给出一组输入。例如:

输出样例:
在这里给出相应的输出。例如:

编号,团险,个险,姓名,性别
1,500,400,职工1,female
3,600,300,职工3,male
2,400,600,职工2,female
4,800,200,职工4,female
5,500,700,职工5,male
编号,团险,个险,姓名,性别
2,400,600,职工2,female
1,500,400,职工1,female
5,500,700,职工5,male
3,600,300,职工3,male
4,800,200,职工4,female
代码长度限制16 KB
时间限制4000 ms
内存限制128 MB

二、涉及到的知识点

1、Comparable接口

使用Comparable必须要修改原有的类,也就是你要排序的那个类
就要在郡个中实现Comparable接口并重写 compareTo方法
所以Comparable更像是“对内”进行排序的接口

  • Comparable接口中只有一个compareTo方法 实现Comparable接口并重写compareTo方法就可以实现某个类的排序了
  • 支持Collections.sort和Arrays.sort的排序
    在这里插入图片描述
    在这里插入图片描述
    如果这题在不实现Comparable接口的情况下可以看见输出结果是这样的
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> ss = new ArrayList<>();
        ss.add(new Staff(1, 500, 400, "职工1", "female"));
        ss.add(new Staff(2, 400, 600, "职工2", "female"));
        ss.add(new Staff(3, 600, 300, "职工3", "male"));
        ss.add(new Staff(4, 800, 200, "职工4", "female"));
        ss.add(new Staff(5, 500, 700, "职工5", "male"));
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }
    }
}
class Staff{
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }

    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

在这里插入图片描述
从上图可以看出

当自定义类Staff没有实现Comparable时,List集合是没有排序的
只能以元素的插入顺序作为输出的顺序

然而这题要求该类根据职工总业绩的比较进行正序排序
在这里插入图片描述
这个时候就可以请出我们本文的主角Comparable接口出场了
Comparable的使用:

是在自定义类中重写CompareTo方法来实现自定义排序规则的

具体实现代码如下:

package PTA.test7_3;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> ss = new ArrayList<>();
        ss.add(new Staff(1, 500, 400, "职工1", "female"));
        ss.add(new Staff(2, 400, 600, "职工2", "female"));
        ss.add(new Staff(3, 600, 300, "职工3", "male"));
        ss.add(new Staff(4, 800, 200, "职工4", "female"));
        ss.add(new Staff(5, 500, 700, "职工5", "male"));
        
        Collections.sort(ss);
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }
    }
}

class Staff implements Comparable<Staff> {
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }

    @Override
    public int compareTo(Staff s) {
        int ret = this.pscore + this.tscore - s.getPscore() - s.getTscore();
        if (ret > 0) ret = 1;
        else if (ret < 0) ret = -1;
        return ret;
    }

    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

在这里插入图片描述
在代码中我们重新了Comparable接口的compareTo方法
compareTo方法说明:

compareTo方法接收的参数s是要对比的对象
排序规则是用当前对象和要对比的对象进行比较
返回一个Int类型的值

正序从小到大的排序规则是:

用当前的对象值减去要对比对象的值;

倒序从大到小的排序规则是:

用对比对象值减去当前对象的值

注意事项:

如果自定义对象没有实现Comparable接口,那么它是不能使用Collections.sort方法进
行排序的

编译器会报错
在这里插入图片描述

2、Comparator接口

Comparator的使用则不相同
Comparator无需修改原有类
也就是在最极端情况下,即使Staff类是第三方提供的
我们依然可以通过创建新的自定义比较器Comparator来实现对第三方类Staff的排序功能
也就是说通过Comparator接口可以实现和原有类的解耦
在不修改原有类的情况下实现排序功能
所以Comparator可以看作是“对外”提供排序的接口

Comparator和Comparable的排序方法是不同的,
Comparable排序的方法是compareTo
而Comparator排序的方法是compare,
在这里插入图片描述

用Comparator实现对团体险业绩升序排序。
具体代码如下:

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> ss = new ArrayList<>();
        ss.add(new Staff(1, 500, 400, "职工1", "female"));
        ss.add(new Staff(2, 400, 600, "职工2", "female"));
        ss.add(new Staff(3, 600, 300, "职工3", "male"));
        ss.add(new Staff(4, 800, 200, "职工4", "female"));
        ss.add(new Staff(5, 500, 700, "职工5", "male"));

        ss.sort(new newComparetor());
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }


    }
}

class Staff {
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }


    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

class newComparetor implements Comparator<Staff> {
    public newComparetor() {
    }

    @Override
    public int compare(Staff o1, Staff o2) {
        int ret = o1.getTscore() - o2.getTscore();
        if (ret > 0) ret = 1;
        else if (ret < 0) ret = -1;
        return ret;
    }
}

在这里插入图片描述

三、整题答案

第一套答案:

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> ss = new ArrayList<>();
        ss.add(new Staff(1, 500, 400, "职工1", "female"));
        ss.add(new Staff(2, 400, 600, "职工2", "female"));
        ss.add(new Staff(3, 600, 300, "职工3", "male"));
        ss.add(new Staff(4, 800, 200, "职工4", "female"));
        ss.add(new Staff(5, 500, 700, "职工5", "male"));

        Collections.sort(ss);
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }
        ss.sort(new newComparetor());
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }


    }
}

class Staff implements Comparable<Staff> {
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }

    @Override
    public int compareTo(Staff s) {
        int ret = this.pscore + this.tscore - s.getPscore() - s.getTscore();
        if (ret > 0) ret = 1;
        else if (ret < 0) ret = -1;
        return ret;
    }

    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

class newComparetor implements Comparator<Staff> {
    public newComparetor() {
    }

    @Override
    public int compare(Staff o1, Staff o2) {
        int ret = o1.getTscore() - o2.getTscore();
        if (ret > 0) ret = 1;
        else if (ret < 0) ret = -1;
        return ret;
    }
}

第二套答案:

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> list = new ArrayList<>();
        list.add(new Staff(1, 500, 400, "职工1", "female"));
        list.add(new Staff(2, 400, 600, "职工2", "female"));
        list.add(new Staff(3, 600, 300, "职工3", "male"));
        list.add(new Staff(4, 800, 200, "职工4", "female"));
        list.add(new Staff(5, 500, 700, "职工5", "male"));
        Collections.sort(list);
        System.out.println("编号,团险,个险,姓名,性别");
        list.forEach(System.out::println);
        Staff[] ss = list.toArray(new Staff[0]);
        Arrays.sort(ss, Comparator.comparing(Staff::getTscore));
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }
    }
}

class Staff implements Comparable<Staff> {
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }

    @Override
    public int compareTo(Staff s) {
        int ret = this.pscore + this.tscore - s.getPscore() - s.getTscore();
        if (ret > 0) ret = 1;
        else if (ret < 0) ret = -1;
        return ret;
    }

    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

四、拓展知识点

1、comparator匿名类

comparator除了可以通过创建自定义比较器外还可以通过匿名类的方式,更快速、便捷的完成自定义比较器的功能

具体的实现代码如下

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> list = new ArrayList<>();
        list.add(new Staff(1, 500, 400, "职工1", "female"));
        list.add(new Staff(2, 400, 600, "职工2", "female"));
        list.add(new Staff(3, 600, 300, "职工3", "male"));
        list.add(new Staff(4, 800, 200, "职工4", "female"));
        list.add(new Staff(5, 500, 700, "职工5", "male"));
        
        list.sort(new Comparator<Staff>() {
            @Override
            public int compare(Staff o1, Staff o2) {
                int ret = o1.getTscore() - o2.getTscore();
                if (ret > 0) ret = 1;
                else if (ret < 0) ret = -1;
                return ret;
            }
        });
        list.forEach(System.out::println);
    }
}

class Staff {
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }


    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

在这里插入图片描述

2、comparator的lambda表达式

代码如下

  list.sort((o1, o2) -> {
            int ret = o1.getTscore() - o2.getTscore();
            if (ret > 0) ret = 1;
            else if (ret < 0) ret = -1;
            return ret;
        });

在这里插入图片描述

3、comparing方法实现

在这里插入图片描述
先用集合自带的toArray方法把list转数组再通过Arrays.sort()传入comparing去比较
关于List的toArray()方法超详细解释可以看看这位博主的文章:
List的toArray()方法

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Staff> list = new ArrayList<>();
        list.add(new Staff(1, 500, 400, "职工1", "female"));
        list.add(new Staff(2, 400, 600, "职工2", "female"));
        list.add(new Staff(3, 600, 300, "职工3", "male"));
        list.add(new Staff(4, 800, 200, "职工4", "female"));
        list.add(new Staff(5, 500, 700, "职工5", "male"));
        Staff[] ss = list.toArray(new Staff[0]);
        Arrays.sort(ss, Comparator.comparing(Staff::getTscore));
        System.out.println("编号,团险,个险,姓名,性别");
        for (Staff s : ss) {
            System.out.println(s.toString());
        }
    }
}

class Staff implements Comparable<Staff> {
    private int number;
    private int tscore;
    private int pscore;
    private String name;
    private String sex;

    public Staff() {
    }

    public Staff(int number, int tscore, int pscore, String name, String sex) {
        this.number = number;
        this.tscore = tscore;
        this.pscore = pscore;
        this.name = name;
        this.sex = sex;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getTscore() {
        return tscore;
    }

    public void setTscore(int tscore) {
        this.tscore = tscore;
    }

    public int getPscore() {
        return pscore;
    }

    public void setPscore(int pscore) {
        this.pscore = pscore;
    }

    @Override
    public int compareTo(Staff s) {
        int ret = this.pscore + this.tscore - s.getPscore() - s.getTscore();
        if (ret > 0) ret = 1;
        else if (ret < 0) ret = -1;
        return ret;
    }

    @Override
    public String toString() {
        return number +
                "," + tscore +
                "," + pscore +
                "," + name +
                "," + sex;
    }
}

总结

Comparable和Comparator都是用来实现元素排序的

区别如下:

  1. mparable是“比较”的意思,而Comparator是“比较器”的意思;
  2. Comparable是通过重写compareTo方法实现排序的,而Comparator是通过重写compare方法实现排序的;
  3. Comparable必须由自定义类内部实现排序方法,而Comparator是外部定义并实现排序的

一句话总结:
Comparable是“对内”进行排序的接口,Comparator是“对外”提供排序的接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值