GeeCON Prague 2014*
The moose likes Java in General and the fly likes Weird Behaviour of List.contains() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Weird Behaviour of List.contains()" Watch "Weird Behaviour of List.contains()" New topic
Author

Weird Behaviour of List.contains()

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
I found that List.contains() does not return true even if 2 objects are equals(). Here is my test code. Wrapper is my class wrapping some other objects. My List is a list of Wrapper and I want two wrappers be considered equals if they're actually wrapping the same underlying object. According to javadoc. List.contains() should use the element's equals() to test if the list is containing it. But my test seems it does not. Here is my code:





And the result:

test.Superclass@1fb8ee3
test.Superclass@61de33
test.Superclass@14318bb
test.Superclass@ca0b6
false
test.Superclass@1fb8ee3
test.Superclass@1fb8ee3 true
test.Superclass@61de33 false
test.Superclass@14318bb false
test.Superclass@ca0b6 false



So the line System.out.println(wrapperList.contains(new Wrapper(s1) )); return false??? But why ?

From javadoc for List.contains():


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

Joined: Oct 13, 2005
Posts: 39084
    
  23
Your equals() method does not test for equality but identity between two objects. You must correctly override the equals() method, which sounds easy an isn't, or List#contains() to give the expected results.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
And on closer examination, you haven't even tried to override the equals() method. You have overloaded it.
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Since I want 2 wrappers be considered equal if they are wrapper around the same object instance
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Campbell Ritchie wrote:And on closer examination, you haven't even tried to override the equals() method. You have overloaded it.


ok,thanks, that is the cause of the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You're welcome

Always remember the @Override annotation.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I always prefer to just return the results of the comparison rather than adding redundant logic:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Then I'm really glad you didn't write String, Integer or many of the other classes of the core API.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Me too, but my point was the syntax, not the implementation in this case.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
That "equals" you quoted will not maintain the general contract for equals; you are testing whether the "o" passed is the same object as the "o" field in your current object. So the two things you are claiming to compare are guaranteed to be different in, but may return true. Rob was correct to imply there is poor logic here.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
I think David meant something like

His point was just that there's not need for

when this is sufficient:

It's just that his condition was a little off.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Geez, I was just using the original poster's logic :(
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
Yes, but you also replaced "this.o==w.o" with "this.o == o" (where the original w had also been renamed as o). This resulted in different logic, which is what Campbell was on about, and maybe Rob as well. The instanceof and casting I added were just fallout from the original error on overriding equals() correctly; no biggie.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, didn't even see that.
 
GeeCON Prague 2014
 
subject: Weird Behaviour of List.contains()