Compare objects in LinkedList.contains()

2019-01-15 08:41发布

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

Suppose that I have 1 LinkedList and 2 objects

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)

( a == b ) == true

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

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?

6条回答
\"骚年 ilove
2楼-- · 2019-01-15 08:47
( a == b ) == true

Did you mean a.equals(b) and b.equals(a) return true? This is not the same as a check for reference equality, nor a check for 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.

查看更多
仙女界的扛把子
3楼-- · 2019-01-15 08:49

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-15 08:52

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

查看更多
Lonely孤独者°
5楼-- · 2019-01-15 08:55

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

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

Also a really good book to read is Effective Java

查看更多
走好不送
6楼-- · 2019-01-15 08:57

The documentation for the contains method is as follows:

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

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

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.

查看更多
时光不老,我们不散
7楼-- · 2019-01-15 09:12

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.

查看更多
登录 后发表回答