TreeSet使用详情

在这里插入图片描述
自己创建的Person类


public class Person implements Comparable {//实现Comparable接口,要重写compareTo方法
    String name;
    int age;

    public Person() {

    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;

        Person person = (Person) o;

        if (age != person.age) return false;
        return name != null ? name.equals(person.name) : person.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
    //按照名字由小到大排列并且年龄从小到大重写compare方法
    @Override
    public int compareTo(Object o) {
        if(o instanceof Person){
            Person p=(Person)o;
            int tem=this.name.compareTo(p.name);
 //如果我们不加这一部分,那么相同名字的后来add的就不能加上
            if(tem!=0){
                return tem;
                //return -tem;名字从大到小排序
            }else{
                return Integer.compare(this.age,p.age);
            }
        }else{
            throw new RuntimeException("数据不存在");
        }

    }
}

自然排序

public class Test1 {
    public static void main(String[] args){
       TreeSet set=new TreeSet();
       set.add(new Person("Jack",12));
        set.add(new Person("Tom",23));
        set.add(new Person("Jok",56));
        set.add(new Person("吴嘉欣",18));
        set.add(new Person("Tom",56));
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
         System.out.println(iterator.next());
        }
    }
}

运行结果
在这里插入图片描述

定制排序

public class Test1 {

    public static void main(String[] args){
        //按年龄从小到大排序
        Comparator com=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Person && o2 instanceof Person){
                    Person p1=(Person)o1;
                    Person p2=(Person)o2;
                    return Integer.compare(p1.getAge(),p2.getAge());
                }else{
                    throw new RuntimeException();
                }
            }
        };
       TreeSet set=new TreeSet(com);//传进com就按定制排序,若不传就按自然排序
       set.add(new Person("Jack",12));
        set.add(new Person("Tom",23));
        set.add(new Person("Jok",56));
        set.add(new Person("吴嘉欣",18));
        set.add(new Person("Tom",56));
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
         System.out.println(iterator.next());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值