JAVA LIST 根据对象元素去重

应用场景:在开发中过程中 会存在根据List集合中的对象一个或者多个元素进行去重 

1:根据List集合中的对象一个元素进行去重 

List organizationPurchaseHeadList = purchaseHeadList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
        Comparator.comparing(PurchaseHead::getOrganizationId)
)), ArrayList::new));

2:根据List集合中的对象多个元素进行去重

List storageLotLockNumList = lotTypeLockNumList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
        Comparator.comparing(o -> o.getGoodsId() + ";" + o.getLotNumber())
)), ArrayList::new));

3:List去重

 goodsIdList = goodsIdList.stream().distinct().collect(Collectors.toList());

你可能感兴趣的:(java)