在java中,有段stream流,notifyInfoCCDetail.stream() .map(passengerDetailInfo -> new PassengerDetailInfoPhoneNbr( passengerDetailInfo.getPassengerPhoneNbr(), passengerDetailInfo.getEcifPhoneNbr(), passengerDetailInfo.getAgentPhoneNbr(), passengerDetailInfo.getCustomPhoneNbr(), passengerDetailInfo.getChnName(), passengerDetailInfo.getOldFltDt(), passengerDetailInfo.getOldDptAirportCd(), passengerDetailInfo.getOldArvAirportCd(), passengerDetailInfo.getOldFltNbr(), passengerDetailInfo.getNewFltDt(), passengerDetailInfo.getNewDptAirportCd(), passengerDetailInfo.getNewArvAirportCd(), passengerDetailInfo.getNewFltNbr() )) .distinct() .forEach(phoneList::add);。现在通过 .distinct()指定PassengerPhoneNbr,EcifPhoneNbr,AgentPhoneNbr,CustomPhoneNbr,ChnName实现去重,将完整代码展示出来
时间: 2024-03-20 21:40:17 浏览: 19
Java中的`java.util.stream.Collectors.toMap()`方法有什么作用
下面是完整的Java代码:
```
notifyInfoCCDetail.stream()
.map(passengerDetailInfo -> new PassengerDetailInfoPhoneNbr(
passengerDetailInfo.getPassengerPhoneNbr(),
passengerDetailInfo.getEcifPhoneNbr(),
passengerDetailInfo.getAgentPhoneNbr(),
passengerDetailInfo.getCustomPhoneNbr(),
passengerDetailInfo.getChnName(),
passengerDetailInfo.getOldFltDt(),
passengerDetailInfo.getOldDptAirportCd(),
passengerDetailInfo.getOldArvAirportCd(),
passengerDetailInfo.getOldFltNbr(),
passengerDetailInfo.getNewFltDt(),
passengerDetailInfo.getNewDptAirportCd(),
passengerDetailInfo.getNewArvAirportCd(),
passengerDetailInfo.getNewFltNbr())
)
.distinct((p1, p2) -> p1.getPassengerPhoneNbr().equals(p2.getPassengerPhoneNbr())
&& p1.getEcifPhoneNbr().equals(p2.getEcifPhoneNbr())
&& p1.getAgentPhoneNbr().equals(p2.getAgentPhoneNbr())
&& p1.getCustomPhoneNbr().equals(p2.getCustomPhoneNbr())
&& p1.getChnName().equals(p2.getChnName())
)
.forEach(phoneList::add);
```
在这个代码中,我们在 `distinct()` 方法中传入了一个比较器,这个比较器会比较 `PassengerDetailInfoPhoneNbr` 对象的 `PassengerPhoneNbr`、`EcifPhoneNbr`、`AgentPhoneNbr`、`CustomPhoneNbr` 和 `ChnName` 属性,只有这些属性都相等的两个对象才会被认为是相等的。这样就可以保证我们得到的列表中不会有重复的对象了。
阅读全文