在java数组列表中查找重复对象 [英] Find Duplicate Objects in an java arraylist
问题描述
首先,我想说我已经搜索了这个问题的答案,但没有找到合适的解决方案.所以我决定把它贴在这里.
First of all, I would like to say that I have searched for an answer to this, but did not get an suitable solution. So I have decided to post it here.
我有一个对象的 ArrayList (ArrayList(Provider)).提供者对象具有名字、姓氏、NPI 编号、列表(我没有列出所有字段).
I have an ArrayList of Objects (ArrayList(Provider)). The Provider Object has First Name, Last Name, NPI Number, List (I have not listed all the fields).
Provider {
private long providerId;
private String npiNumber;
private PersonName providerName;
private List<Address> providerAddresses;
}
现在我想根据这些属性(名字、姓氏、NPI、地址)查找 ArrayList 是否有重复项.每个提供者将有 2 个地址.我们遇到的问题是提供者对象是从 XSD 生成的,无法修改.所以我不能覆盖 equals 和 hashcode 方法.所以 Hashset(list) 不起作用.
Now I want to find if the ArrayList has duplicates based on these attributes (First Name, Last Name, NPI, Addresses). Each Provider will have 2 addresses. The issue we have is the Provider Object is generated from XSD and cannot be modified. So I cannot override the equals and hashcode methods. So The Hashset(list) does not work.
那么检查 ArrayList 是否有重复对象的最佳方法是什么.请告诉我
So what is the best way to check if the ArrayList has duplicate objects. Please let me know
谢谢
苦涩
推荐答案
你可以创建一个带有自定义 Comparator
的 TreeSet
或者 TreeMap 如果您想知道重复项是什么.
You can create a TreeSet<Provider>
with a custom Comparator<Provider>
Or a TreeMap if you want to know what the duplicates are.
这篇关于在java数组列表中查找重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!