About TreeSet sorting to determine whether objects are duplicated and what should be done

import java.util.Iterator;
import java.util.TreeSet;

public class Main {

public static void main(String[] args){
    TreeSet<ClassMate> classMates = new TreeSet<>();
    classMates.add(new ClassMate("Lilith", 10, 6));
    classMates.add(new ClassMate("jack", 10, 6));
    classMates.add(new ClassMate("jack", 10, 6));
    classMates.add(new ClassMate("Adeam", 5, 7));

    Iterator it = classMates.iterator();
    it.forEachRemaining(e -> System.out.println(e.toString()));
}

}

class ClassMate implements Comparable < ClassMate > {

private String name;
private int age;
private int grade;

@Override
public String toString() {
    return "name:" + this.name + "\t" + "age:" + this.age + "\t" + "grade:" + this.grade;
}

public ClassMate(String name, int age, int grade){
    this.name = name;
    this.age = age;
    this.grade = grade;
}

public void setAge(int age) {
    this.age = age;
}

public int getAge() {
    return age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}

@Override
public int compareTo(ClassMate o) {
    if (this.age > o.age){
        return 1;
    }else if (this.age < o.age){
        return -1;
    }else
        return this.name.compareTo(o.name);
}

}

Mar.03,2021

override the compareTo method to determine whether an object is duplicated

 public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check

            root = new Entry<>(key, value, null);
            size = 1;
            modCountPP;
            return null;
        }
        int cmp;
        Entry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        else {
            if (key == null)
                throw new NullPointerException();
            @SuppressWarnings("unchecked")
                Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        Entry<K,V> e = new Entry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        sizePP;
        modCountPP;
        return null;
    }

compareTo

Menu