本文介绍了我能够在TreeSet中插入重复的条目。如何克服这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个叫做Employee的类,它有 employeeName 和 employeeId 作为其成员变量。我正在创建新员工对象,然后将其添加到 TreeSet 中,我想根据 employeeId 对其进行排序。但我认为如果两个Employee对象具有相同的 employeeName ,则它们是相等的。设置不允许重复。但在这里我可以观察到一种奇怪的行为。这是我的代码。(我这里没有使用getter和setter。我直接访问成员变量。)I have a class called Employee which has employeeName and employeeId as its member variables.I am creating new Employee objects and then adding it into a TreeSet where I want to sort it based on the employeeId. But I consider 2 Employee objects to be equal if they have the same employeeName. Set does not allow duplicates. But here I can observe a strange behavior. This is my code.(I am not using getters and setters here. I am directly accessing the member variables.)package secondOne;import java.util.Set;import java.util.TreeSet;class Employee implements Comparable<Employee> { String employeeName; int employeeId; public Employee(String name, int id) { this.employeeName = name; this.employeeId = id; } public int compareTo(Employee emp) { //return this.employeeName.compareTo(emp.employeeName); return (this.employeeId - emp.employeeId); } @Override public String toString() { return ("Name is: " + employeeName + " Emp id is: " + employeeId); } @Override public boolean equals(Object emp) { if (emp instanceof Employee && ((Employee) emp).employeeName == this.employeeName) { return true; } return false; }}public class TestingSetsWithComparable { /** * @param args */ public static void main(String[] args) { Employee e1 = new Employee("A", 1); Employee e2 = new Employee("A", 2); Employee e3 = new Employee("B", 3); Set<Employee> set = new TreeSet<Employee>(); set.add(e1); set.add(e2); set.add(e3); System.out.println(set); }}这里上面代码的输出是, [姓名是:一个Emp ID是:1,名称是:一个Emp id是:2,名字是:B Emp id是:3] Here the output for the above code is,[Name is: A Emp id is: 1, Name is: A Emp id is: 2, Name is: B Emp id is: 3]我的第一个问题是,在equals()方法中,如果他们有相同的employeeName,我认为2个Employee objests是相等的但是在compareTo方法中,我使用employeeId进行排序。在这种情况下,输出显示employeeName'A'的2个条目。当我认为2个对象具有相同的employeeName时,TreeSet如何允许重复条目。这怎么可能..? 第二个问题是,在compareTo方法中,如果我使用employeeName进行排序,那么我就不会获得同名的第二个重复条目。第二种情况下的输出是 [名称是:一个Emp id是:1,名称是:B Emp id是:3] My first question is, in equals() method, I consider 2 Employee objests to be equal if they have same employeeName but in compareTo method, I am using employeeId to sort. In this case, output is showing 2 entries for employeeName 'A'. How is TreeSet allowing a duplicate entry when I consider 2 objects to be same if they have the same employeeName. How is this possible..? And Second question is, in the compareTo method, if I use employeeName to sort, then I dont get the second repeated entry for the same name. The output in this second case is[Name is: A Emp id is: 1, Name is: B Emp id is: 3]为什么会这样..?推荐答案问题在于: ((Employee)emp).employeeName== this.employeeName您必须使用等于方法比较 String s :You must compare Strings using equals method:((Employee)emp).employeeName.equals(this.employeeName)请参阅如何比较Java中的字符串? 另外,既然你要覆盖等于的方法,那么覆盖 hashCode 方法,如 对象#equals 合同:Also, since you're overriding equals method, it would be good if you override hashCode method as well, as stated in Object#equals contract:补充:由于你使用 TreeSet ,它将使用 compareTo 方法而不是等于和 hashCode 方法。这是因为 TreeSet 实现了 SortedSet 接口。请参阅 SortedSet javadoc(强调我的):Additional: Since you're using TreeSet, it will use the compareTo method instead of the equals and hashCode methods. This is because TreeSet implements SortedSet interface. Refer to SortedSet javadoc (emphasis mine):您应该根据需要实施此方法:You should implement this method in the accordingly to your needs:public int compareTo(Employee emp) { if (this.employeeName.equals(emp.employeeName)) { return 0; } //removed the comparison by subtraction since it will behave wrongly on int overflow return new Integer(this.employeeId).compareTo(emp.employeeId);}由于您正在比较字符串,我建议使用 StringUtils Apache Commons Lang 的课程,提供帮助方法以避免 null 支票和其他。Since you're comparing Strings, I would recommend using StringUtils class from Apache Commons Lang that provides helper methods to avoid null checks and others. 这篇关于我能够在TreeSet中插入重复的条目。如何克服这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 07:44