Java 比较 LinkedList.contains() 中的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/549808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:07:04  来源:igfitidea点击:

Compare objects in LinkedList.contains()

javalinked-listcontainsequality

提问by Eric

I want to be able to have LinkedList.contains() return true for a custom comparator.

我希望能够让 LinkedList.contains() 为自定义比较器返回 true。

Suppose that I have 1 LinkedList and 2 objects

假设我有 1 个 LinkedList 和 2 个对象

LinkedList<MyObject> myList = new LinkedList<MyObject>();

MyObject a = new MyObject("HELLO");
MyObject b = new MyObject("HELLO");

Technicaly, both objects are identical in terms of comparison (MyObject implements Comparable)

从技术上讲,两个对象在比较方面是相同的(MyObject 实现了 Comparable)

( a == b ) == true

( a == b ) == 真

however, when I do the following, myList does not return true for myList.contains(b)

但是,当我执行以下操作时,myList 不会为 myList.contains(b) 返回 true

myList.add(a)
myList.contains(b) // == false

I think its because contains will check object reference and see that a and b are 2 distinct objects. Is there any way I can make it so I don't have to extend LinkedList to compare those objects?

我认为这是因为 contains 将检查对象引用并看到 a 和 b 是 2 个不同的对象。有什么办法可以让我不必扩展 LinkedList 来比较这些对象?

采纳答案by Brett Daniel

LinkedList uses the equals method, not Comparable.compareTo. You should override equals (and hashCode)in MyObject to solve the problem.

LinkedList 使用 equals 方法,而不是 Comparable.compareTo。您应该覆盖MyObject 中的equals(和 hashCode)来解决问题。

回答by Zach Scrivena

( a == b ) == true

Did you mean a.equals(b)and b.equals(a)return true? This is not the sameas a check for reference equality, nor a check for a.compareTo(b) == 0.

你的意思是a.equals(b)b.equals(a)返回true?这检查引用相等性或检查a.compareTo(b) == 0.

LinkedList.contains()uses equals(), so you have to make sure that the method has been implemented correctly. equals()should also be consistent with compareTo(), though this is not strictly necessary. If you're using a hash-based data structure (e.g. HashSet), you must ensure that hashCode()is implemented correctly.

LinkedList.contains()使用equals(),因此您必须确保该方法已正确实施equals()也应该与 一致compareTo(),尽管这不是绝对必要的。如果您使用的是基于散列的数据结构(例如HashSet),则必须确保hashCode()正确执行

回答by Luke Woodward

The contains()method uses equals()to determine whether an object is in the list. I suspect your class MyObjectdoes not override the equals()method, and this will be why myList.contains(b)is returning false.

contains()方法用于equals()确定对象是否在列表中。我怀疑您的类MyObject没有覆盖该equals()方法,这myList.contains(b)就是返回false.

回答by mweiss

The documentation for the contains method is as follows:

contains 方法的文档如下:

Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

如果此集合包含指定的元素,则返回 true。更正式地说,当且仅当此集合包含至少一个元素 e 使得 (o==null ? e==null : o.equals(e)) 时才返回 true。

Therefore, you need to override the MyObject's equals(Object o) method.

因此,您需要覆盖 MyObject 的 equals(Object o) 方法。

So for your example:

所以对于你的例子:

public class MyObject {
  String myVal;

  public boolean equals(Object o ) {
    return ((MyObject)o).myVal.equals(myVal);
  }
}

You do not need to implement anything with the Comparable interface.

您不需要使用 Comparable 接口实现任何内容。

回答by TofuBeer

You need to override the .equals(Oject) and the .hashCode() methods in the MyObject class (hashCode isn't needed for the List... but when you overrite equals the contract says you have to override hashCode).

您需要覆盖 MyObject 类中的 .equals(Oject) 和 .hashCode() 方法(列表不需要 hashCode...但是当您覆盖 equals 时,合同说您必须覆盖 hashCode)。

Essentially what the contains does is this:

基本上包含的作用是这样的:

for(each item in the list)
{
    if(theCurrentItem.equals(theItemYouAreLookingFor))
    {
        return (true);
    }
}

return (false);

Take a look at the documentation for Object (for equals and hashCode) here

此处查看 Object(对于 equals 和 hashCode)的文档

Also a really good book to read is Effective Java

另外一本非常值得阅读的书是Effective Java

回答by Peter Lawrey

Rather than use a LinkedList to search through every element, Have you considered using a new HashSet(Comparator). This will efficiently compare the elements to find a match.

而不是使用 LinkedList 来搜索每个元素,您是否考虑过使用新的 HashSet(Comparator)。这将有效地比较元素以找到匹配项。