Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicates from a list?

I want to remove duplicates from a list but what I am doing is not working:

List<Customer> listCustomer = new ArrayList<Customer>();     for (Customer customer: tmpListCustomer) {   if (!listCustomer.contains(customer))    {     listCustomer.add(customer);   }  } 
like image 771
Mercer Avatar asked May 17 '10 13:05

Mercer


2 Answers

If the code in your question doesn't work, you probably have not implemented equals(Object) on the Customer class appropriately.

Presumably there is some key (let us call it customerId) that uniquely identifies a customer; e.g.

class Customer {     private String customerId;     ... 

An appropriate definition of equals(Object) would look like this:

    public boolean equals(Object obj) {         if (obj == this) {             return true;         }         if (!(obj instanceof Customer)) {             return false;         }         Customer other = (Customer) obj;         return this.customerId.equals(other.customerId);     } 

For completeness, you should also implement hashCode so that two Customer objects that are equal will return the same hash value. A matching hashCode for the above definition of equals would be:

    public int hashCode() {         return customerId.hashCode();     } 

It is also worth noting that this is not an efficient way to remove duplicates if the list is large. (For a list with N customers, you will need to perform N*(N-1)/2 comparisons in the worst case; i.e. when there are no duplicates.) For a more efficient solution you could use a HashSet to do the duplicate checking. Another option would be to use a LinkedHashSet as explained in Tom Hawtin's answer.

like image 39
Stephen C Avatar answered Oct 10 '22 04:10

Stephen C


Assuming you want to keep the current order and don't want a Set, perhaps the easiest is:

List<Customer> depdupeCustomers =     new ArrayList<>(new LinkedHashSet<>(customers)); 

If you want to change the original list:

Set<Customer> depdupeCustomers = new LinkedHashSet<>(customers); customers.clear(); customers.addAll(dedupeCustomers); 
like image 155
Tom Hawtin - tackline Avatar answered Oct 10 '22 04:10

Tom Hawtin - tackline