1

A complete newbie here so I apologize if my question is stupid. I honestly tried before posting. I have a list of some customers, with their customer Id's as one column and customer name as the other, and age as the third column.

I want to go through this list and identify whether the same customerId is in the list more than once. In that case, I need to delete that whole customer (even if his name or age is different).

Can you please advise what to use to do this logic?

I tried adding the customers to a set (because set would not add duplicates), but how do I state that it is the customerId, not the customer, that cannot be duplicated in this list?

So far I got this below, but in my logic nothing says that customer is considered a duplicate when his customerId is a duplicate. (I don't necessarily need to use a list. Customer is an object).

   //Customer is a class that contains a private variable customerId, so I   can do customer.getCustomerId();

    List<Customer> notDuplicatedCustomers = new ArrayList<Customer>(); //list  of customers
    final Set<Customer> setToReturn = new HashSet<Customer>();
    final Set<Customer> initialSet = new HashSet<Customer>();

    for (Customer customer: notDuplicatedCustomers ) {
    if (!initialSet.add(customer)) {
    setToReturn.add(customer);
       } 
    }
Amie Chu
  • 31
  • 6

5 Answers5

1

I tried adding the customers to a set (because set would not add duplicates), but how do I state that it is the customerId, not the customer, that cannot be duplicated in this list?

This is possible if you override the equals() method based on the ID field, thereby telling the set that two customers are logically equal if they have the same ID. Here is an example implementation generated by my IDE (assuming id is of type String; if the type is some primitive type use its wrapper):

@Override
public boolean equals(final Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Student other = (Student) obj;
    if (id == null) {
        if (other.id!= null) {
            return false;
        }
    } else if (!id.equals(other.id)) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = (prime * result) + ((id == null) ? 0 : id.hashCode());
    return result;
}

final Set<Customer> myCustomerSet = new HashSet<Customer>();
myCustomerSet.add(customer1);
...

Then when you add customers to the set you should have only one entry with the same ID in the set. The hashCode() is not necessary for your case, but normally if you override equals(), you should also override the hashCode().

ujulu
  • 3,289
  • 2
  • 11
  • 14
0

You can use HapMap for this,

List<Customer> lstCustomer = new ArrayList<Customer>(); // list of customer
Map<Integer,Customer> map = new HashMap<Integer, Customer>(); //here integer is your id type

for(int i=0;i<lstCustomer .size();i++){
    if(map.get(lstCustomer .get(i).getId())==null){
        map.put(lstCustomer .get(i).getId(),lst.get(i));
    }else{
        System.out.println(lstCustomer .get(i).getId()); // duplicate customer id
    }
}

Hope it helps you.

PSabuwala
  • 155
  • 1
  • 9
0

Create a TreeSet with a custom comparator as an input paramater. inside comparator write your equals logic. Now add each customer to treeset. The final treeset will not have duplicates.

0

You can write a custom method using inbuild function: frequency()

Sample Code: // Let us create a list with 4 items ArrayList list = new ArrayList(); list.add("Id100"); list.add("Id101"); list.add("Id100"); list.add("Id100");

    // count the frequency of the duplicate Id "Id100" 
    System.out.println(Collections.frequency(list, "Id100"));  
0
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.*;

public class RemoveDuplicatesArrayListMain {

    public static void main(String[] args) {
        ArrayList employeeNameList = new ArrayList();
        employeeNameList.add("John");
        employeeNameList.add("Ankit");
        employeeNameList.add("Rohan");
        employeeNameList.add("John");
        employeeNameList.add("Amit");
        employeeNameList.add("Ankit");

        System.out.println("Removing duplicates from list:");
        // Using iterative approach
        ArrayList uniqueElements = new ArrayList();
        for (String empName : employeeNameList) {

            if (!uniqueElements.contains(empName)) {
                uniqueElements.add(empName);
            }
        }

        System.out.println("Using iterative approach:");
        for (String uniqElem : uniqueElements) {
            System.out.println(uniqElem);
        }
        System.out.println("*******************************");
        System.out.println("Using HashSet :");
        // using HashSet but does not maintain order
        uniqueElements = new ArrayList(new HashSet(
                employeeNameList));
        for (String uniqElem : uniqueElements) {
            System.out.println(uniqElem);
        }
        System.out.println("*******************************");
        System.out.println("Using LinkedHashSet :");
        // using LinkedHashSet maintaining order
        uniqueElements = new ArrayList(new LinkedHashSet(
                employeeNameList));
        for (String uniqElem : uniqueElements) {
            System.out.println(uniqElem);
        }

    }
}