This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird Behaviour of List.contains()

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And on closer examination, you haven't even tried to override the equals() method. You have overloaded it.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I want 2 wrappers be considered equal if they are wrapper around the same object instance
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Always remember the @Override annotation.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always prefer to just return the results of the comparison rather than adding redundant logic:
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I'm really glad you didn't write String, Integer or many of the other classes of the core API.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too, but my point was the syntax, not the implementation in this case.
 
Campbell Ritchie
Sheriff
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Geez, I was just using the original poster's logic :(
 
Mike Simmons
Ranch Hand
Posts: 3032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, didn't even see that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic