10

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?

Quinn Taylor
44.8k16 gold badges116 silver badges135 bronze badges
asked Feb 14, 2009 at 21:09

6 Answers 6

27

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

answered Feb 14, 2009 at 21:16
Sign up to request clarification or add additional context in comments.

3 Comments

What if I can't do that? is there a Set that gets a custom comparator?
Any idea why the @Override tag says I'm not overriding anything when I define an equals method in MyObject?
Figured it out: method definition public boolean equals(MyObject obj) doesn't trigger the override, but public boolean equals(Object obj) does.
3

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.

answered Feb 14, 2009 at 21:16

Comments

3

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

answered Feb 14, 2009 at 21:18

Comments

2
( 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.

answered Feb 14, 2009 at 21:16

Comments

2

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.

answered Feb 14, 2009 at 21:17

Comments

1

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.

answered Feb 15, 2009 at 7:26

1 Comment

HashSet is not overloaded to take a Comparator object in it's constructor. Did you mean a TreeSet?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.