1、按照对象中的一个属性去重 //明细详情 List<ReceiptDetailListBack> receiptDetailListBackList1 = receiptBackBody.getDetailList(); //去重进库任务明细状况 List<ReceiptDetailListBack> receiptBackList= receiptDetailListBackList1.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getDetailID()))), ArrayList::new) ); unique.forEach(p -> logger.info("去重后的数据{}",p));
2、按照对象的多个属性去重
//明细详情 List<ReceiptDetailListBack> receiptDetailListBackList1 = receiptBackBody.getDetailList(); //去重进库任务明细状况 List<ReceiptDetailListBack> receiptBackList= receiptDetailListBackList1.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getDetailID()+ ";" + o.receiptCode()))), ArrayList::new) ); unique.forEach(p -> logger.info("去重后的数据{}",p));