简体   繁体   English

Java如何在对象列表中保留唯一值

[英]Java How To Keep Unique Values In A List of Objects

I currently am retrieving a list of objects List<NprDto> (The NprDto class contains accountId, theDate1, and theDate2) from a query that returns results where the NprDto has duplicate accountIds. 我目前正在从查询中检索对象List<NprDto> (NprDto类包含accountId,theDate1和theDate2),该查询返回NprDto具有重复accountIds的结果。 I need to have a List<NproDto> of only unique accountIds but keep the object. 我需要一个只有唯一accountIds的List<NproDto> ,但保留该对象。 It only needs to add the first accountId it comes across and ignores the rest. 它只需要添加它遇到的第一个accountId并忽略其余的。

I'm currently trying this: 我正在尝试这个:

private List<NprDto> getUniqueAccountList(List<NprDto> nonUniqueAccountList) throws Exception {

    Map<Long,NprDto> uniqueAccountsMapList = new HashMap<Long,NprDto>();
    List<NprDto> uniqueAccountsList = null;

    if(nonUniqueAccountList != null && !nonUniqueAccountList.isEmpty()) {
        for(NprDto nprDto : nonUniqueAccountList) {
            uniqueAccountsMapList.put(Long.valueOf(nprDto.getAccountId()), nprDto);
        }
    }

    uniqueAccountsList = new ArrayList<NprDto>(uniqueAccountsMapList.values());

    return uniqueAccountsList;

}

But this doesn't seem to be working because when I iterate through the returned uniqueAccountsList later it only picks up the first object. 但这似乎没有用,因为当我迭代返回的uniqueAccountsList后,它只会拾取第一个对象。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I need to have a List of only unique accountIds but keep the object. 我需要一个只有唯一的accountIds列表,但保留对象。

You should use Set<NprDto> . 您应该使用Set<NprDto> For that you need to override equals and hasCode at NproDto class. 为此,您需要在NproDto类中覆盖equalshasCode

class NprDto{
   Long accountId;
   .......

 @Override
 public boolean equals(Object obj) {
   NproDto other=(NproDto) obj;
   return this.accountId==other.accountId;
 }

 @Override
 public int hashCode() {
    return accountId.hashCode();
 }
}

Change your getUniqueAccountList as follows: 更改你的getUniqueAccountList ,如下所示:

private Set<NprDto> getUniqueAccountSet(){    
  Map<Long,NprDto> uniqueAccountsMapList = new HashMap<Long,NprDto>();
  Set<NprDto> uniqueAccs = new HashSet<NprDto>(uniqueAccountsMapList.values());    
  return uniqueAccs;
}

What you need here is a LinkedHashSet . 你需要的是LinkedHashSet It removes duplicates and keeps insertion order. 它删除重复项并保持插入顺序。 You do not need TreeSet here because it sorts and changes the order of the original List . 不需要 TreeSet这里,因为它排序,并改变原来的顺序List

If preserving insertion order is not important use a HashSet . 如果保留插入顺序并不重要,请使用HashSet

Actually you need to implements equals and hascode method, it will be good for you 实际上你需要实现equals和hascode方法,这对你有好处

Remove duplicates from a list 从列表中删除重复项

Java Set contains the Unique value but its unsorted collection. Java Set包含Unique值但其未排序的集合。 List is sorted collection but contains the duplicates objects. 列表是已排序的集合,但包含重复项对象。

What you need to do is implement the equals , hashCode and compareTo methods for NprDto to match two objects as equal when their ID is the same. 你需要做的是为NprDto实现equalshashCodecompareTo方法,以便在它们的ID相同时将两个对象匹配为相等。 Then you can filter all duplicates as easy as this: 然后,您可以像这样简单地过滤所有重复项:

private List<NprDto> getUniqueAccountList(List<NprDto> nonUniqueAccountList) {
    return new ArrayList<NprDto>(new LinkedHashSet<NprDto>(nonUniqueAccountList));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM