| Author |
ArrayList.Contains always return true, even the objects are not equal
|
Jaykiran Teraiya
Greenhorn
Joined: Jun 21, 2010
Posts: 12
|
|
Hi All,
I am using an ArrayList to store my CustomQuery objects. I am using .contains(customQuery) to compare weather the object is already present or not? If it's not there I am adding the object to the list.
The problem is list.contains(customQuery) always return false, even if the objects are equal(i.e. their id's are equal). I've also overrided .equals() and .hascode() method in my CustomQuery class.
Any help would be highly appriciated.
Thanks
Jay
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
What will happen if you use equals() method instead of using == in checking the customerQueryId?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Jaykiran Teraiya
Greenhorn
Joined: Jun 21, 2010
Posts: 12
|
|
|
Thanks, Worked like a charm..
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
And Welcome to JavaRanch!
|
 |
Jaykiran Teraiya
Greenhorn
Joined: Jun 21, 2010
Posts: 12
|
|
Thank you for warm welcome.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Two things:
1) what will happen if I pass a String into the equals method? It will certainly compile.
2) do you really need a List? A Set makes sure of uniqueness; if the order is important then a LinkedHashSet will work as well.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jaykiran Teraiya
Greenhorn
Joined: Jun 21, 2010
Posts: 12
|
|
|
my first choice was to use a Set, but I need to interact with other API and it accepts list as it's parameter. If I use set I need to convert set into list , so to avoid that conversion I used List.
|
 |
 |
|
|
subject: ArrayList.Contains always return true, even the objects are not equal
|
|
|