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. I need to have a List<NproDto> of only unique accountIds but keep the object. It only needs to add the first accountId it comes across and ignores the rest.
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.
Any help would be greatly appreciated.