Java Basics - Collections Framework --Set - TreeSet ranking

A sort concept:.
TreeSet collection have underlying red-black tree algorithm, will use the default storage elements natural ordering (small to large).
Note: You must ensure TreeSet elements of the object in the collection is the same data type, otherwise an error.

Two natural ordering:
natural ordering ( from small to large ):
TreeSet call compareTo method set elements to compare the size relation of elements, then speaks the set of elements in ascending order (from small to large).
Note: Requires TreeSet elements in the set have implemented java .util.Comparable interfaces.
Here Insert Picture Description
java.util.Comparable Interface: comparable
cover public int compareTo (Object o) method, comparison rules written in this method.
in this method, the comparison of the current object (this) and the parameter object o compare (strictly speaking is the comparison of data object, such as age sorted by object).
the this> O: returns a positive integer. 1.
the this <O: negative integer -1.
the this == O: this returns 0 when two objects are considered as the same object.
in the natural order of the TreeSet that if two objects being compared compareTo method returns zero is considered to be the same object.

class Person implements Comparable<Person>{
    private String name;
    private Integer age;

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person o) {
        if(this.age>o.age){
            return 1;
        }else if(this.age<o.age){
            return -1;
        }
        return 0;
    }
}
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>();
        set.add(new Person("赵一",20));
        set.add(new Person("钱二",30));
        set.add(new Person("孙三",10));
        set.add(new Person("李四",15));
        System.out.println(set);
        //[Person{name='孙三', age=10}, Person{name='李四', age=15}, Person{name='赵一', age=20}, Person{name='钱二', age=30}]
    }
}

Three custom ordering:
passing java.lang.Comparator TreeSet object constructor and covered public int compare (Object o1, Object o2) and then write comparison rules.
Here Insert Picture Description

class Person {
     String name;
     Integer age;

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class TreeSetDemo1 {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>(new NameComparator());
        set.add(new Person("陈伊伊",20));
        set.add(new Person("钱二",30));
        set.add(new Person("孙三三",10));
        set.add(new Person("李四四四",15));
        System.out.println(set);
        //[Person{name='李四四四', age=15}, Person{name='孙三三', age=10}, Person{name='陈伊伊', age=20}, Person{name='钱二', age=30}]
    }
}
class NameComparator implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        //先按照名字字数的长短来比较
        if(o1.name.length()>o2.name.length()){
            return -1;
        }else if(o1.name.length()<o2.name.length()){
            return 1;
            //如果名字长短一样,再按照年龄来自然排序
        }else if(o1.age>o2.age){
            return 1;
        }else if(o1.age<o2.age){
            return -1;
        }
        return 0;
    }
}

For TreeSet collection, the use of either natural ordering, or use a custom ordering.
Determines whether two objects are equal rules:
natural ordering: the compareTo method returns 0;
custom ordering: compare method returns 0;

Published 99 original articles · won praise 2 · Views 2604

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105309851