Java List集合根据某字段去重
作者:互联网
去重方法
单个字段为条件去重
/**
* 单字段去重
* @param jackpotList1 新集合
* @param jackpotList 需要去重的集合
* @return
*/
private List<Jackpot> distinctList1(List<Jackpot> jackpotList1, List<Jackpot> jackpotList) {
jackpotList1.addAll(jackpotList);
return jackpotList1.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Jackpot::getPrizeId))),ArrayList::new
)
);
}
多个字段为条件去重
/**
* 多字段去重
* @param jackpotList1 新集合
* @param jackpotList 需要去重的集合
* @return
*/
private List<Jackpot> distinctList(List<Jackpot> jackpotList1, List<Jackpot> jackpotList) {
jackpotList1.addAll(jackpotList);
return jackpotList1.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(jackpot -> jackpot.getMyOrderId() + ";" + jackpot.getPrizeId()))),ArrayList::new
)
);
}
标签:jackpotList,jackpotList1,return,某字,List,param,new,Java 来源: https://www.cnblogs.com/aerfazhe/p/15710314.html