集合-Comparator和Comparable比较器使用

说明:

        1.Comparator和Comparable都是用来比较数据的接口。

        2.排序方式相同。

 方法:

        compare(Object o1, Object o2)

                o1:集合中的数据,o2:即将添加的数据。

        compareTo(Person p)

                返回值为0,添加的元素重复,去重。

                返回值为-1,添加的元素比较大,放在右子树。

                返回值为1:添加的元素比较小,放在左子树。

使用方式:

        自定义类实现Comparator接口,重写comparaeTo方法。

package demos1_treeset;
 
public class Person implements Comparable<Person>{
    private String name;
    private int age;
 
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public Person() {
    }
 
    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;
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
 
    @Override
    public int compareTo(Person o) {
  
        //想法1:按照年龄从小到大排列
        //this.age;集合中已经存在的元素年龄(默认循环获取  二叉树中已经存在的元素)
        // o.age;即将要添加的元素的年龄
        //return this.age-o.age;
        //想法2:按照年龄从大到小排列
        //return o.age-this.age;
        //想法3:按照名字(String)的默认排序方式(自然排序)排列
        //String类型已经默认实现Comparable接口,默认重写了compareto
        //return this.name.compareTo(o.name);
        //想法4:先按照年龄升序排列。如果年龄相同,再按照姓名默认排列
        int result1 = this.age-o.age;
        int result2 = this.name.compareTo(o.name);
        return result1 == 0? result2 :result1;
    }
}

        创建TreeSet集合对象时创建Comparator接口的实现类对象,且重写compare方法。

        Student.java(实体类):

package com.study.enety.demo14;

public class Student{
    private String name;
    private int chinese;
    private int math;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public float getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                '}';
    }
    public int getSum() {
        return chinese + math;
    }
}

        Test.java(测试类)

package com.study.test;
import com.study.enety.demo12.Person;
import com.study.enety.demo14.Student;

import java.util.Comparator;
import java.util.TreeSet;


public class Demo14Test {
    public static void main(String[] args) {

        Comparator<Student> com = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result1 =  (o1.getSum() - o2.getSum());
                int result2 = o1.getChinese()-o2.getChinese();
                int result3 = o1.getName().compareTo(o2.getName());
                //先按照总成绩,总成绩相同,再按照语文成绩,语文成绩相同,按照名字排序
                return result1 == 0 ?(result2 == 0 ? result3 : result2) : result1;
            }
        };


        TreeSet<Student> tr = new TreeSet<Student>(com);
        tr.add(new Student("张三",90,100));
        tr.add(new Student("李四",80,13));
        tr.add(new Student("王五",65,100));
        tr.add(new Student("赵六",90,100));
        System.out.println(tr);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值