TreeSet通过实现Comparator接口,自定义元素排序

//1:定义一个StudentScore类

package cn.dqlai.day10.demo03;

public class StudentScore {

    private String name;
    private float chinese;
    private float math;

    public StudentScore() {
    }

    public StudentScore(String name, float chinese, float math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    public String getName() {
        return name;
    }

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

    public float getChinese() {
        return chinese;
    }

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

    public float getMath() {
        return math;
    }

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

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

//2:定义ComparatorImpl实现类实现Comparator接口,自定义元素排序规则

package cn.dqlai.day10.demo03;

import java.util.Comparator;

public class ComparatorImpl implements Comparator<StudentScore> {

    //需求:排序规则[比如:先按学生的chinese从小至大,如果相同,则按学生的math从小到大,若相同,则比较name]
    @Override
    public int compare(StudentScore o1, StudentScore o2) {
        float flag = o1.getChinese() - o2.getChinese();
        if (flag > 0) {
            return 1;
        } else if (flag < 0) {
            return -1;
        } else {
            float flag2 = o1.getMath() - o2.getMath();
            if (flag2 > 0) {
                return 1;
            } else if (flag2 < 0) {
                return -1;
            } else {
//                int compareTo(String anotherString)
//                按字典顺序比较两个字符串。
                return o1.getName().compareTo(o2.getName());
            }
        }
    }
}

//3:创建一个测试类Test03

package cn.dqlai.day10.demo03;

import java.util.TreeSet;

public class Test03 {

    public static void main(String[] args) {

        ComparatorImpl comparator = new ComparatorImpl();//定义一个Comparator接口的实现类对象
//    TreeSet(Comparator<? super E> comparator)
//    构造一个新的空 TreeSet,它根据指定比较器进行排序。
        TreeSet<StudentScore> set = new TreeSet<>(comparator);//以实现类对象comparator初始化TreeSet类对象set,设置排序规则[比如:先按学生的chinese从小至大,如果相同,则按学生的math从小到大,若相同,则比较name]

        StudentScore[] arr = {
                new StudentScore("dqlai",87.5F,78.5F),
                new StudentScore("wl",89.5F,76.5F),
                new StudentScore("liHao",84.5F,82.5F),
        };

        for(int i=0;i<arr.length;i++){
            set.add(arr[i]);
        }
        System.out.println(set);//输出结果:[StudentScore{name='liHao', chinese=84.5, math=82.5}, StudentScore{name='dqlai', chinese=87.5, math=78.5}, StudentScore{name='wl', chinese=89.5, math=76.5}]
        //结果按照学生的chinese属性从小到大排序输出

        StudentScore s4 = new StudentScore("zym",87.5F,78.5F);//s4对象的chinese和math属性和set集合中的name为dqlai的对应的属性相同
        set.add(s4);
        System.out.println(set);//输出结果:[StudentScore{name='liHao', chinese=84.5, math=82.5}, StudentScore{name='dqlai', chinese=87.5, math=78.5}, StudentScore{name='zym', chinese=87.5, math=78.5}, StudentScore{name='wl', chinese=89.5, math=76.5}]
        //结果s4对象按照name比较排在dqlai对象的后面

        StudentScore s5 = new StudentScore("aym",87.5F,80.5F);
        set.add(s5);
        System.out.println(set);//输出结果:[StudentScore{name='liHao', chinese=84.5, math=82.5}, StudentScore{name='dqlai', chinese=87.5, math=78.5}, StudentScore{name='zym', chinese=87.5, math=78.5}, StudentScore{name='aym', chinese=87.5, math=80.5}, StudentScore{name='wl', chinese=89.5, math=76.5}]
        //结果s5对象根据math排在s4对象zym之后

    }

}

//总结:
1:TreeSet存储自定义数据类型时,必须实现Comparator接口或让自定义类实现Comparable接口,否则TreeSet集合不知道按何种排序规则输出元素,将会抛出异常;
2:public int compare(Object o1, Object o2) {
o1.属性 - o2.属性:
(1)>0,o2—>o1;
(2)<0,o1—>o2;
(3)=0,TreeSet集合认为重复,去重
}
3:由2中的(3)可以知道,TreeSet的去重原理,是在添加一个元素的时候,将集合set中已有的元素与其按照排序规则比较,若返回值为0,则认为重复,不添加;

//验证3:
//1:重写Comparator接口的实现类ComparatorImpl,排序规则[比如:先按学生的chinese从小至大,如果相同,则按学生的math从小到大,若相同,则为重复元素,不添加]

package cn.dqlai.day10.demo03;

import java.util.Comparator;

public class ComparatorImpl implements Comparator<StudentScore> {

    //需求:排序规则[比如:先按学生的chinese从小至大,如果相同,则按学生的math从小到大,若相同,则为重复元素,不添加]
    @Override
    public int compare(StudentScore o1, StudentScore o2) {
        float flag = o1.getChinese() - o2.getChinese();
        if (flag > 0) {
            return 1;
        } else if (flag < 0) {
            return -1;
        } else {
            float flag2 = o1.getMath() - o2.getMath();
            if (flag2 > 0) {
                return 1;
            } else if (flag2 < 0) {
                return -1;
            } else {
                return 0;
            }
        }
    }
}

//2:重写测试类Test02

package cn.dqlai.day10.demo03;

import java.util.TreeSet;

public class Test03 {

    public static void main(String[] args) {

        ComparatorImpl comparator = new ComparatorImpl();//定义一个Comparator接口的实现类对象
//    TreeSet(Comparator<? super E> comparator)
//    构造一个新的空 TreeSet,它根据指定比较器进行排序。
        TreeSet<StudentScore> set = new TreeSet<>(comparator);//以实现类对象comparator初始化TreeSet类对象set,设置排序规则[比如:先按学生的chinese从小至大,如果相同,则按学生的math从小到大,若相同,则比较name]

        StudentScore[] arr = {
                new StudentScore("dqlai",87.5F,78.5F),
                new StudentScore("wl",89.5F,76.5F),
                new StudentScore("liHao",84.5F,82.5F),
        };

        for(int i=0;i<arr.length;i++){
            set.add(arr[i]);
        }
        System.out.println(set);//输出结果:[StudentScore{name='liHao', chinese=84.5, math=82.5}, StudentScore{name='dqlai', chinese=87.5, math=78.5}, StudentScore{name='wl', chinese=89.5, math=76.5}]
        //结果按照学生的chinese属性从小到大排序输出

        StudentScore s4 = new StudentScore("zy",89.5F,76.5F);//输出结果:false
        System.out.println("s4是否添加成功:"+set.add(s4));
        System.out.println(set);//输出结果:[StudentScore{name='liHao', chinese=84.5, math=82.5}, StudentScore{name='dqlai', chinese=87.5, math=78.5}, StudentScore{name='wl', chinese=89.5, math=76.5}]
        //结果没有添加s4,因为s4的chinese和math和wl都相同,被认为是重复的元素;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值