My code declares a Tree Set object, only to find that the "same" element has been added. Excuse me, why?

topic description

sources of topics and their own ideas

my program declares a TreeSet object ts and writes a student class that is considered the same element when the student"s age and name are the same. When the first student object and the last student object are added to the ts, the names and ages of the two student objects are the same, and the print results show that both elements are output with a ts size of 4.

related codes

import java.util.TreeSet;
public class TreeSetDemo {

public static void main(String[] args) {
    TreeSet<Student> ts = new TreeSet<>();
    ts.add(new Student("lisi02", 22));
    ts.add(new Student("lisi01", 40));
    ts.add(new Student("lisi08", 19));
    ts.add(new Student("lisi02", 22));


    // the first element and the last one are added to ts
    // However, ts belongs to a Set Collection.
    // So I think the last one should not be added to ts.
    // when the second element is annotated, the last one can not be added.
    // Can you explain why?
    
    for (Student e : ts) {
        System.out.println(e.getName() + "\t...\t" + e.getAge());
    }
    System.out.println(ts.size());
}

}

class Student implements Comparable {

private String name;
private int age;
Student(String name, int age) {
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

public int getAge() {
    return age;
}

public int compareTo(Object obj) {
    if (!(obj instanceof Student))
        throw new RuntimeException("Not Student class");
    Student p = (Student) obj;

    // When name and age are the same, the elements are the same
    if (this.name.equals(p.getName()) && p.getAge() == this.age) {
        System.out.println(name + "..." +age);
        return 0;
    } else
        return 1;
}

}

what result do you expect? What is the error message actually seen?

Apr.24,2022

  1. your compareTo method returns 0 or 1 , returns 0 when the new value replaces the old value, and
    returns 1 , it is added to the right side of the red-black tree as a child node.
  2.  ts.add(new Student("lisi02", 22));
     ts.add(new Student("lisi01", 40));
     ts.add(new Student("lisi08", 19));
     ts.add(new Student("lisi02", 22));

    if the code is like this, when you add the third element lisi08 19 , the red-black tree will rotate, and the root node is lisi01 40 , the left child node is lisi02 22 , and the right child node is lisi08 19 . When you add the fourth element, it will only be compared with lisi01 40 and lisi08 19 , because your compareTo method does not have the return value of -1 . So the fourth element is saved in ts.

  3.  ts.add(new Student("lisi02", 22));
     // ts.add(new Student("lisi01", 40));
     ts.add(new Student("lisi08", 19));
     ts.add(new Student("lisi02", 22));

    when you comment out the second element and add lisi02 22 and lisi08 19 , the red-black tree does not need to rotate. The root node is lisi02 22 , and the right child node is lisi08 19 . When you add the last lisi02 22 , it will be compared with the first lisi02 22 . So there will only be two elements in the ts.


where does the same element come from? To determine whether the elements are the same, you need to implement the hashCode and equals methods, which are obviously not implemented, so the first element and the last element are different elements. Comparable is used to compare size and sort. It's like a truck and a truck can be compared in size, but are they the same?


TreeSet is compared by Comparator passed at creation time.

TreeSet(Comparator<? super E> comparator)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b370de-2c068.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b370de-2c068.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?