gpt4 book ai didi

java - TreeSet.contains() 不会调用被覆盖的 equals

转载 作者:行者123 更新时间:2023-12-03 21:48:01 34 4
gpt4 key购买 nike

我对 TreeSet 的 contains() 方法有问题。据我了解, contains() 应该像 javadoc 所说的那样调用包含对象的 equals() :

boolean java.util.TreeSet.contains(Object o): Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).



我尝试做的事情:
我有一个带有成员的结果对象的 TreeSet 列表 String baseword .现在我想将每个 TreeSet 与所有其他树集进行比较,并为每对创建一个它们共享的基本词列表。为此,我为 treeSet1 遍历列表一次第二次是 treeSet2 ,然后我遍历 treeSet2 中的所有 ResultObjects并运行 treeSet1.contains(ResultObject)对于每一个,看看是否 treeSet1包含具有此词库的结果对象。我调整了 compareToequals ResultObject 的方法。不过好像我的 equals永远不会被调用。
谁能解释我为什么这不起作用?

你好,
丹尼尔
    public static void getIntersection(ArrayList<TreeSet<Result>> list, int value){

for (TreeSet<Result> treeSet : list){

//for each treeSet, we iterate again through the list of TreeSet, starting at the TreeSet that is next
//to the one we got in the outer loop
for (TreeSet<Result> treeSet2 : list.subList((list.indexOf(treeSet))+1, list.size())){

//so at this point, we got 2 different TreeSets
HashSet<String> intersection = new HashSet<String>();

for (Result result : treeSet){
//we iterate over each result in the first treeSet and see if the wordbase exists also in the second one

//!!!
if (treeSet2.contains(result)){
intersection.add(result.wordbase);
}
}
if (!intersection.isEmpty()){
intersections.add(intersection);
}
}
}


public class Result implements Comparable<Result>{

public Result(String wordbase, double result[]){
this.result = result;
this.wordbase = wordbase;
}

public String wordbase;
public double[] result;

public int compareTo(DifferenceAnalysisResult o) {

if (o == null) return 0;

return this.wordbase.compareTo(o.wordbase);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((wordbase == null) ? 0 : wordbase.hashCode());
return result;
}

//never called
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DifferenceAnalysisResult other = (DifferenceAnalysisResult) obj;
if (wordbase == null) {
if (other.wordbase != null)
return false;
} else if (!wordbase.equals(other.wordbase))
return false;
return true;
}
}

最佳答案

As I understand it, contains() should call equals() of the contained Objects


不适用于 TreeSet , 不。它调用 compare :

A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

...

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface.


您的 compareTo方法目前与 equals 不一致- x.compareTo(null)返回 0,而 x.equals(null)返回假。也许你对此没意见,但你不应该期待 equals被称为。

关于java - TreeSet.contains() 不会调用被覆盖的 equals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25198475/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com