Does Customer implement the equals()
contract?
If it doesn't implement equals()
and hashCode()
, then listCustomer.contains(customer)
will check to see if the exact same instance already exists in the list (By instance I mean the exact same object--memory address, etc). If what you are looking for is to test whether or not the same Customer( perhaps it's the same customer if they have the same customer name, or customer number) is in the list already, then you would need to override equals()
to ensure that it checks whether or not the relevant fields(e.g. customer names) match.
Note: Don't forget to override hashCode()
if you are going to override equals()
! Otherwise, you might get trouble with your HashMaps and other data structures. For a good coverage of why this is and what pitfalls to avoid, consider having a look at Josh Bloch's Effective Java chapters on equals()
and hashCode()
(The link only contains iformation about why you must implement hashCode()
when you implement equals()
, but there is good coverage about how to override equals()
too).
By the way, is there an ordering restriction on your set? If there isn't, a slightly easier way to solve this problem is use a Set<Customer>
like so:
Set<Customer> noDups = new HashSet<Customer>();
noDups.addAll(tmpListCustomer);
return new ArrayList<Customer>(noDups);
Which will nicely remove duplicates for you, since Sets don't allow duplicates. However, this will lose any ordering that was applied to tmpListCustomer
, since HashSet
has no explicit ordering (You can get around that by using a TreeSet
, but that's not exactly related to your question). This can simplify your code a little bit.