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);
}
}