JAVA Foundation (TreeSet stores custom object comparison and sorting)

1. TreeSet stores custom objects

  • The Comparable method must be implemented in the bean class storing the Person object. (implements compatible)

  • The TreeSet collection is used to sort the object elements, and also to ensure the uniqueness of the elements

  • When the compareTo method returns 0, there is only one element in the collection

  • When the compareTo method returns a positive number, the collection can be retrieved as long as it is stored.

  • When the compareTo method returns a negative number, the collection is stored in reverse order

 

2. Code implementation

[1] diagram

  • Sort by age

 

 

  • Tree. First, compare with compareTo. The negative number is on the left. Positive numbers are on the right. Output results. Li Si, Zhang San, Zhao Liu, Wang Wu.

 


 

public static void demo2() {

        TreeSet<Person> ts = new TreeSet<>();

        ts.add(new Person("Zhang San", 23));

        ts.add(new Person("Li Si", 13));

        ts.add(new Person("Zhou Qi", 13));

        ts.add(new Person("Wang Wu", 43));

        ts.add(new Person("Zhao Liu", 33));

        

        System.out.println(ts);

    }



[2] bean class


 

public class Person implements Comparable<Person> {

    private String name;

    private int age;

    public Person() {

        super();

        

    }

    public Person(String name, int age) {

        super();

        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 obj) {

        System.out.println("Has it been executed? ";

        Person p = (Person)obj;

        return this.name.equals(p.name) && this.age == p.age;

    }

    @Override

    public int hashCode() {

        final int NUM = 38;

        return name.hashCode() * NUM + age;

    }*/

    

    /*

     * Why 31?

     * 1,31 It's a prime number. A prime number is a number that can be divided by 1 and itself.

     * 2,31 This number is neither small nor small

     * 3,31 This number is easy to calculate. The fifth power of 2 - 1,2 moves 5 bits to the left.

     */

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + age;

        result = prime * result + ((name == null) ? 0 : name.hashCode());

        return result;

    }

    @Override

    public boolean equals(Object obj) {

        if (this == obj)                        //The called object and the passed in object are the same object

            return true;                        //Return true directly

        if (obj == null)                        //null object passed in

            return false;                        //Return to false

        if (getClass() != obj.getClass())        //Determine whether the bytecode file corresponding to two objects is the same bytecode

            return false;                        //If not, return false directly

        Person other = (Person) obj;            //Downward transformation

        if (age != other.age)                    //The age of the calling object is not equal to the age of the incoming object

            return false;                        //Return to false

        if (name == null) {                        //The name of the calling object is null

            if (other.name != null)                //The name of the incoming object is not null

                return false;                    //Return to false

        } else if (!name.equals(other.name))    //The name of the calling object is not equal to the name of the incoming object

            return false;                        //Return to false

        return true;                            //Return to true

    }

    /*@Override

    //Sort by age

    public int compareTo(Person o) {

        int num = this.age - o.age;                //Age is the main condition for comparison

        return num == 0 ? this.name.compareTo(o.name) : num;//Name is a secondary condition of comparison

    }*/

    /*@Override

    //Sort by name

    public int compareTo(Person o) {

        int num = this.name.compareTo(o.name);        //Name is the main condition

        return num == 0 ? this.age - o.age : num;    //Age is a secondary condition

    }*/

    /*

     * aaa

     * bbb

     */

    public int compareTo(Person o) {

        int length = this.name.length() - o.name.length();                //Comparison length is the main condition

        int num = length == 0 ? this.name.compareTo(o.name) : length;    //Comparison content is secondary condition

        return num == 0 ? this.age - o.age : num;                        //Age comparison is a secondary condition

    }

    

}

 

 

Posted on Fri, 01 Nov 2019 02:55:43 -0700 by jonabomer