Java——TreeSet中Comparable/Comparator比较器

一、TreeSet中比较器:

  1)Comparable:实体类实现Comparable接口,重写compareTo方法;

  2)Comparator:写一个实现类实现Comparator接口,重写compare方法;创建TreeSet时候传入该实体类的对象;

  3)匿名实现Comparator;

  4)lambdo实现Comparator;

二、Comparable:

  1、实体类:

 

public class Person implements Comparable {
    private String name;
    private int age;
    Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    //实现Comparable接口,重写compareTo方法;
    public int compareTo(Object obj){
        if (!(obj instanceof Person)){
            throw new RuntimeException("不是正确对象");
        }
        Person p=(Person)obj;
        if (p.age>this.age)return -1;
        if (p.age==this.age){
            return this.name.compareTo(p.name);
        };
        return 1;
    }
}

 

  2、测试类:

public class Test {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet();
        ts.add(new Person("zs0", 20));
        ts.add(new Person("zs1", 20));
        ts.add(new Person("zs1", 21));
        ts.add(new Person("zs9", 19));

        for (Iterator<Person> it = ts.iterator(); it.hasNext(); ) {
            Person p = it.next();
            System.out.println(p.getName()+"..."+p.getAge());
        }
    }
}

三、Comparator:

  1、实体类:

public class Student {
    private String name;
    private int age;
    public Student(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 "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  2、comparator实现类:

 

public class ComparatorImpl implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        if (o1==o2)return 0;
        if (!(o1 instanceof Student) || !(o2 instanceof Student)){
            throw new RuntimeException("类型不对");
        }
        Student s1=(Student)o1;
        Student s2=(Student)o2;
        int num=s1.getName().compareTo(s2.getName());
        if (num==0){
            return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
        }
        return num;
    }
}

  3、测试类:

public class Test {
    public static void main(String[] args) {
        TreeSet ts=new TreeSet(new ComparatorImpl());
        ts.add(new Student("z1", 1));
        ts.add(new Student("z3", 3));
        ts.add(new Student("z2", 2));
        ts.add(new Student("z3", 2));
        ts.add(new Student("z3", 2));

        Iterator it=ts.iterator();
        while (it.hasNext()){
            System.out.println(it.next().toString());
        }
    }
}

四、comparator匿名实现:

 

public class Test2 {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 == o2) return 0;
                if (!(o1 instanceof Student) || !(o2 instanceof Student)) {
                    throw new RuntimeException("类型不对");
                }
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;
                int num = s1.getName().compareTo(s2.getName());
                if (num == 0) {
                    return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                }
                return num;
            }
        });
        ts.add(new Student("z1", 1));
        ts.add(new Student("z3", 3));
        ts.add(new Student("z2", 2));
        ts.add(new Student("z3", 2));
        ts.add(new Student("z3", 2));

        Iterator it = ts.iterator();
        while (it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }
}

五、lambda实现comparator:

  //lambda需要jdk1.8;

public class Test3 {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet((o1, o2)->{
            if (o1 == o2) return 0;
            if (!(o1 instanceof Student) || !(o2 instanceof Student)) {
                throw new RuntimeException("类型不对");
            }
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            int num = s1.getName().compareTo(s2.getName());
            if (num == 0) {
                return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
            }
            return num;
        });
        ts.add(new Student("z1", 1));
        ts.add(new Student("z3", 3));
        ts.add(new Student("z2", 2));
        ts.add(new Student("z3", 2));
        ts.add(new Student("z3", 2));

        Iterator it = ts.iterator();
        while (it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }
}

 

转载于:https://www.cnblogs.com/Tractors/p/11241583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值