[英]Remove Duplicates With Stream Distinct
@Override
public List<ExpenseListingDTO> getExpenseListByServiceFileId(Long serviceFileId) {
if (Long.valueOf(serviceFileId) != null) {
List<ExpenseListDetail> expenseListDetail = expenseListDetailRepository.findByServiceFileId(serviceFileId);
List<ExpenseList> expenseList = expenseListRepository.findByExpenseListDetailListIn(expenseListDetail);
//
List<ExpenseListingDTO> expenseListDtoList = expenseListMapper.expenseListDTOToExpenseListingDTO(expenseListMapper.entityListToDtoList(expenseList));
expenseListDtoList.parallelStream().forEach(dto -> {
dto.setTotalAmount(
expenseListDetail.stream().filter(s -> s.getOperationType().equals(OperationType.NEW_EXPENSE.getValue()) || s.getOperationType().equals(OperationType.EXPENSE_RETURN.getValue()))
.filter(s -> !s.getStatus().equals(ExpenseListDetailStatus.PASSIVE.getValue())).map(ExpenseListDetail::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
});
return expenseListDtoList.stream()
.distinct()
.collect(Collectors.toList());
}
return Collections.emptyList();
}
這個 function 返回一個列表。 但是在我的 output 上有一些相同的列表。 我想刪除重復項。 但它沒有用。 這里有什么問題?
請查看並實現ExpenseListingDTO#equals
和ExpenseListingDTO#hashCode
方法。 如果ExpenseListingDTO
對象相同,則這些方法應反映這一點。
編輯:根據評論的要求,我可以在此處找到覆蓋這些內容的指南。
public class ExpenseListingDTO {
....
@Override
public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of ExpenseListingDTO or not
"null instanceof [type]" also returns false */
if (!(o instanceof ExpenseListingDTO)) {
return false;
}
// typecast
ExpenseListingDTO expenseListingDTO = (ExpenseListingDTO) o;
// compare other member variables, etc...
return this.XX == expenseListingDTO.XXX;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.