【问题标题】:Identify duplicate customer id's in a list of customers在客户列表中识别重复的客户 ID
【发布时间】:2017-02-28 14:14:09
【问题描述】:

这里是一个完整的新手,所以如果我的问题很愚蠢,我深表歉意。老实说,我在发布之前尝试过。 我有一些客户的列表,其中一列是他们的客户 ID,另一列是客户名称,第三列是年龄。

我想查看此列表并确定同一 customerId 是否多次出现在列表中。在这种情况下,我需要删除整个客户(即使他的姓名或年龄不同)。

您能否建议使用什么来执行此逻辑?

我尝试将客户添加到集合中(因为集合不会添加重复项),但我如何声明不能在此列表中重复的是 customerId,而不是客户?

到目前为止,我在下面得到了这个,但在我的逻辑中,没有任何内容表明当客户的 customerId 是重复的时候,他会被认为是重复的。 (我不一定需要使用列表。客户是一个对象)。

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

【问题讨论】:

  • 使用HashMap&lt;Intger, Customer&gt;
  • 这篇文章对你有用Remove Duplicate

标签: java list collections set hashset


【解决方案1】:
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);
        }

    }
}

【讨论】:

    【解决方案2】:

    您可以使用 inbuild 函数编写自定义方法:frequency()

    示例代码:

    // Let us create a list with 4 items 
        ArrayList<String> list = 
                        new ArrayList<String>(); 
        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"));  
    

    谢谢,

    【讨论】:

      【解决方案3】:

      使用自定义比较器作为输入参数创建 TreeSet。 在比较器里面写你的等于逻辑。 现在将每个客户添加到树集中。最终的树集不会有重复。

      【讨论】:

        【解决方案4】:

        我尝试将客户添加到集合中(因为集合不会添加重复项),但我如何声明不能在此列表中重复的是 customerId,而不是客户?

        如果您基于 ID 字段覆盖 equals() 方法,这是可能的,从而告诉集合如果两个客户具有相同的 ID,则他们在逻辑上是相等的。这是我的 IDE 生成的示例实现(假设 id 的类型为 String;如果类型是一些原始类型,则使用其包装器):

        @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);
        ...
        

        然后,当您将客户添加到集合中时,您应该在集合中只有一个具有相同 ID 的条目。 hashCode() 对于您的情况不是必需的,但通常如果您覆盖 equals(),您还应该覆盖 hashCode()

        【讨论】:

          【解决方案5】:

          您可以为此使用 HapMap,

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

          希望对你有帮助。

          【讨论】:

          • 同意,我们也可以使用 foreach。谢谢
          • 什么是lst?你的意思是lstCustomer
          • 是lstCustomer,需要更换。
          • 你真的回答并没有在我的最上面的评论中添加任何内容
          • 提交后我正忙着回答我看到你的评论。
          猜你喜欢
          • 2011-03-17
          • 2015-07-16
          • 1970-01-01
          • 2014-11-03
          • 1970-01-01
          • 2011-01-27
          • 1970-01-01
          • 2020-08-17
          • 1970-01-01
          相关资源
          最近更新 更多