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());
        }
    }
}

 

posted @ 2019-07-24 23:30  开拖拉机的拉风少年  阅读(691)  评论(0编辑  收藏  举报