单个字段为条件去重
/** * 单字段去重 * @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 ) ); }