Two sorting methods of TreeSet: natural sorting and custom sorting

Two sorting methods of TreeSet: natural sorting and custom sorting

TreeSet is an implementation class of the SortedSet interface. TreeSet can ensure that the collection elements are in a sorted state. TreeSet has two sorting methods: natural sorting and custom sorting. Natural sorting is used by default.

1. Natural order

The custom class is required to implement the java.lang.Comparable interface and override its compareTo(Object obj) abstract method. In this method, specify which attribute of the custom class to sort by. When adding an element to the TreeSet, first compare it according to compareTo(), once it returns 0, although only the value of this attribute of the two objects is the same, the program will think that the two objects are the same, and the latter object cannot be added Come in. Therefore, compareTo() is required to be consistent with hashCode() and equals()!

1.1 Define a student class

//定义一个学生类
public class Student implements Comparable{

    private String name;
    private Integer age;
    public Student() {
        super();
    }
    public Student(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    //当向TreeSet中添加Student类的对象时,依据此方法,确定按照哪个属性排列。
    //先按照年龄进行排序,如果年龄相等再按姓名排序
    @Override
    public int compareTo(Object o) {
        if(o instanceof Student){
            Student student = (Student) o;
            int i = this.age.compareTo(student.age);
            if(i == 0){
                return this.name.compareTo(student.name);
            }else{
                return i;
            }
        }
        return 0;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}

1.2 Add elements to the TreeSet

//自然排序
@Test
public void test1(){
    Set<Student> set = new TreeSet<Student>();
    set.add(new Student("张飞", 27));
    set.add(new Student("关羽", 28));
    set.add(new Student("刘备", 27));
    set.add(new Student("吕布", 29));
    set.add(new Student("赵子龙", 30));

    for(Object obj : set){
        System.out.println(obj);
    }
}

1.3 Operation effect

Write picture description here

2. Custom sort

//定制排序
    @Test
    public void test2(){
        //1.创建一个实现了Comparator接口类的对象
        Comparator<Student> com = new Comparator<Student>() {
            //2.向TreeSet中添加Student类的对象,在此compare()方法中,指明是按照Customer的哪个属性进行排序的
            @Override
            public int compare(Student o1, Student o2) {
                if(o1 instanceof Student && o2 instanceof Student){
                    Student stu1 = (Student)o1;
                    Student stu2 = (Student)o2;
                    int i = stu1.getAge().compareTo(stu2.getAge());
                    if(i == 0){
                        return stu1.getName().compareTo(stu2.getName());
                    }
                    return i;
                }
                return 0;
            }
        }; 
        //3.将此对象作为形参传递给TreeSet的构造器中
        TreeSet<Student> set = new TreeSet<Student>(com);
        //4.向TreeSet中添加Comparator接口中的compare方法中涉及的类的对象。
        set.add(new Student("张飞", 27));
        set.add(new Student("关羽", 28));
        set.add(new Student("刘备", 27));
        set.add(new Student("吕布", 29));
        set.add(new Student("赵子龙", 30));
        set.add(new Student("曹操", 40));
        set.add(new Student("周瑜", 30));
        set.add(new Student("诸葛亮", 30));

        for(Object obj : set){
            System.out.println(obj);
        }
    }

* running result *
Write picture description here

Note: It is purely personal understanding, if there is an error, please correct it!

Guess you like

Origin blog.csdn.net/xiaokanfuchen86/article/details/113429393