首页 > 解决方案 > 如何使用 Java 8 从 List 方法增强这个 Duplicate objects?列表中的对象是嵌套的,这使得它变得复杂

问题描述

我正在尝试从对象列表中过滤掉重复的对象。我正在尝试从传递给该方法的主列表中删除重复的对象,并创建另一个包含这些重复副本的列表。这个问题变得复杂,因为列表中的主要对象包含我们需要检查重复的对象。我的要求有点如下所述:

List<RateContract> "rateContractListWithOptions" contains two objects of RateContract:
[
    RateContract1 :{
      Rate :{tarifId: 1 //other variables will also be defined}
      Contract : {contractId:1}
    },
    RateContract2 :{
      Rate :{tarifId: 2}
      Contract : {contractId:1}
    }
]

Duplicate Rates will be checked using the equals method in the Rate class
At the end of the functions Processing 
"rateContractListWithOptions" this will have only one object of RateContract in list. maybe - [RateContract1 :{
      Rate :{tarifId: 1 //other variables will also be defined}
      Contract : {contractId:1}
    }]

and "duplicateRateContracts" this will contain the duplicate
[RateContract2 :{
      Rate :{tarifId: 2}
      Contract : {contractId:1}
    }]

我已经编写了 filterDuplicateRatesInSameContracts 方法,如何增强它?

    public class RateContract implements Serializable {

    private Rate rate = null;
    private Contract contract = null;
    private Map<Integer,List<Option>> optionMap = new HashMap<>();
    private Map<String, String> otherInformationMap = new HashMap<>();

    }

    public class Rate implements Serializable {

    private String promoCode = null;
    private String tiers_groupe_id = null;
    private String business_model = null;
    private Integer tarifId = null;
    private Integer ageMin = null;
    private Integer ageMinAbs = null;
    private String fuelType = null;

    @Override
    public boolean equals(Object o) {

        if (o == null || getClass() != o.getClass()) return false;
        Rate rate = (Rate) o;
        return Objects.equals(promoCode, rate.promoCode) &&
                Objects.equals(business_model, rate.business_model) &&
                !Objects.equals(tarifId, rate.tarifId) &&
                Objects.equals(ageMin, rate.ageMin) &&
                Objects.equals(ageMinAbs, rate.ageMinAbs) &&
                Objects.equals(fuelType, rate.fuelType) &&
                Objects.equals(ageMax, rate.ageMax) &&
                Objects.equals(ageMaxAbs, rate.ageMaxAbs);
    }

    @Override
    public int hashCode() {
        return Objects.hash(promoCode, business_model, tarifId, ageMin, ageMinAbs, fuelType, ageMax, ageMaxAbs);
    }
    }


    public class Contract implements Serializable {

    private Integer contractId; 
    ......
    }

    //The filtering Logic method is::

     private List<RateContract> filterDuplicateRatesInSameContracts(List<RateContract> rateContractListWithOptions) {
        Map<Integer, List<RateContract>> rateContractMap = new HashMap<>();
        rateContractListWithOptions.forEach(rateContract -> {
            rateContractMap.computeIfAbsent(rateContract.getContract().getContractId(), k -> new ArrayList<>()).add(rateContract);
        });
        List<RateContract> duplicateRateContracts = new ArrayList<>();
        rateContractMap.forEach((contract, rateContracts) -> {
            if (rateContracts.size() > 1) {
                for (RateContract rateContract : rateContracts) {
                    boolean isFound = false;
                    for (RateContract dupliRateContract : duplicateRateContracts) {
                        if (rateContract.getRate().equals(dupliRateContract.getRate())) {
                            isFound = true;
                            break;
                        }
                    }
                    if (!isFound) duplicateRateContracts.add(rateContract);
                }
            }
        });
        rateContractListWithOptions.removeAll(duplicateRateContracts);
        return duplicateRateContracts;
        }

标签: javalistarraylistforeachjava-stream

解决方案


我将您的问题解释为“如何将列表中与列表中较早项目具有相同费率的所有合同移至单独的列表”?

如果是这样:

List<RateContract> duplicates = new ArrayList<>();
Set<Rate> rates = new HashSet<>();
Iterator<RateContract> iterator = contracts.iterator();
while (iterator.hasNext()) {
    RateContract contract = iterator.next();
    if (!rates.add(contract.getRate())) {
        iterator.remove();
        duplicates.add(contract);
    }
}

请注意,这样Iterator您可以在列表中移动时删除具有匹配费率的合同。另一种方法是将它们收集在重复列表中,然后将它们删除。两者都会起作用。


推荐阅读