Does the client equals() contract?
If it does not implement equals() and hashCode() , then listCustomer.contains(customer) checks if the same instance exists in the list (for example, I mean the exact address of the object - memory, etc.). If what you are looking for is to check if there is the same Client in this list (possibly the same client, if they have the same client name or client number), then you will need to override equals() , to ensure that it checks to see if the corresponding fields (for example, customer names) match.
Note. Remember to override hashCode() if you are going to override equals() ! Otherwise, you may have problems with your HashMaps and other data structures. For a good coverage of the reasons for this and what can be avoided, consider looking at the Josh Bloch chapters Effective Java on equals() and hashCode() (The link contains only information on why you should implement hashCode() when implementing equals() , but there is a good idea on how to override equals() too).
By the way, is there a limit on your set? If this does not happen, it is a little easier to solve this problem, use Set<Customer> like this:
Set<Customer> noDups = new HashSet<Customer>(); noDups.addAll(tmpListCustomer); return new ArrayList<Customer>(noDups);
It will be nice to delete duplicates for you, since the sets do not allow duplicates. However, this will lose any ordering that has been applied to tmpListCustomer , since the HashSet does not have an explicit ordering (you can get around this with the TreeSet , but this is not entirely related to your question). This may simplify your code a bit.
Scott Fines May 17 '10 at 13:51 2010-05-17 13:51
source share