Java 对于同一对象,Equals方法不返回true
我的目标是:Java 对于同一对象,Equals方法不返回true,java,spring,Java,Spring,我的目标是: @IdClass=(value = TripleKey.class) class Triple { String subject; String predicate; String object; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getCl
@IdClass=(value = TripleKey.class)
class Triple {
String subject;
String predicate;
String object;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Triple triple = (Triple) o;
if (!subject.equals(triple.subject)) return false;
return predicate.equals(triple.predicate);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + subject.hashCode();
result = 31 * result + predicate.hashCode();
return result;
}
}
{
“主题”:http://www.someurl.com/thing/resources/#SenMLJSON",
“谓词”:“在中探测”,
“对象”:http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#clay"
}
及
{
“主题”:http://www.someurl.com/thing/resources/#SenMLJSON",
“谓词”:“在中探测”,
“对象”:http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#sand"
}
当我尝试以下操作时,仍有重复项:
{
"subject": "http://www.someurl.com/thing/resources/<owner>#SenMLJSON",
"predicate": "probes_in",
"object":"http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#sand"
}
公共静态列表mergeTripleList(列表oldList,列表newList){
Set tripleSet=新哈希集(旧列表);
tripleSet.removeAll(新列表);
tripleSet.addAll(新列表);
返回新的ArrayList(tripleSet);
}
问题出在:
public static List<Triple> mergeTripleLists(List<Triple> oldList, List<Triple> newList) {
Set<Triple> tripleSet = new HashSet<>(oldList);
tripleSet.removeAll(newList);
tripleSet.addAll(newList);
return new ArrayList<>(tripleSet);
}
如果在卸下后仍能正常工作。问题在于:
public static List<Triple> mergeTripleLists(List<Triple> oldList, List<Triple> newList) {
Set<Triple> tripleSet = new HashSet<>(oldList);
tripleSet.removeAll(newList);
tripleSet.addAll(newList);
return new ArrayList<>(tripleSet);
}
If应该在删除它之后工作。问题是调用使用对象引用测试相等性的超类的equals方法,所以删除带有
if (!super.equals(o)) return false;
您还需要删除对超类的hashCode方法的调用。问题是对使用对象引用测试相等性的超类的equals方法的调用,因此删除带有
if (!super.equals(o)) return false;
您还需要删除对超类的hashCode方法的调用。我看不到对equals的调用。您是否调试过它并检查了它在何处或为什么会给出不同的结果?我没有看到对equals的调用。你调试过它并检查过它在哪里或者为什么会产生不同的结果吗?奇怪的是它是用inteliJ->generate->equals hashcode->IntelliJ生成的Default@Arno_Geismar正如其他答案所说,你需要删除
super.hashCode
方法。奇怪的是它是用inteliJ->generate->equals hashCode->IntelliJ生成的Default@Arno_Geismar并且,正如在其他答案中所说的,您需要删除super.hashCode
方法。