java set检查重复_关于TreeSet 排序判断对象是否重复,应该怎么做

import java.util.Iterator;

import java.util.TreeSet;

public class Main {

public static void main(String[] args){

TreeSet 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 {

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);

}

}

你可能感兴趣的:(java,set检查重复)