Java集合中使用"比较器"的集合

本文介绍了Java集合中的TreeSet和TreeMap在使用比较器时的原理和实践。首先展示了在内部实现Comparable接口的情况下如何创建和使用TreeSet,接着讨论了外部Comparator比较器的创建和应用。文章指出,在内部比较器和外部比较器共存时,会优先执行外部比较器。最后,文章提到了TreeMap的比较方式与TreeSet类似,但存储和输出方式有所不同,且在使用比较器时,无需重写equals()和hashCode()方法。
摘要由CSDN通过智能技术生成

TreeSet和TreeMap

本章节仅展示TreeSet

展示在内部比较器下显示的结果

1.1 创建Student类并实现内部比较器

import java.util.Objects;
/**
 * 创建一个Student的javaBean对象   实现内部比较器
 */
public class Student implements Comparable<Student>{
    private int sno; //学号
    private String name; //姓名
    private int age; //年龄
    private int score; //成绩

    public Student() {
    }
    public Student(int sno, String name, int age, int score) {
        this.sno = sno;
        this.name = name;
        this.age = age;
        this.score = score;
    }
    public int getSno() {
        return sno;
    }
    public void setSno(int sno) {
        this.sno = sno;
    }
    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;
  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值