views:

2339

answers:

6

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?

+10  A: 

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

Brett Daniel
What if I can't do that? is there a Set that gets a custom comparator?
Ehrann Mehdan
+2  A: 
( 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.

Zach Scrivena
+2  A: 

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.

Pourquoi Litytestdata
+1  A: 

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.

mweiss
+3  A: 

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

TofuBeer
+1  A: 

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.

Peter Lawrey