0

I am trying to filter out the duplicate Objects from a List of Objects. I am trying to remove the duplicate objects from the main List passed to the method and create another List containing those duplicate copies. This problem becomes complex as The Main Object in the List contains objects within it with which we need to check the duplicate. My requirement is somewhat as described below:

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}
    }]

I have written filterDuplicateRatesInSameContracts method, how do I enhance this?

    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;
        }
11
  • 3
    Possible duplicate of Remove duplicate from List java8 Commented May 16, 2019 at 5:48
  • RateContract has inner object that will define its Duplicacy, The Solution you suggested is plain Object Commented May 16, 2019 at 6:01
  • I think you're probably creating some confusion by using the word 'duplicate'. That normally means items that are equal. In your case I think you want to remove any RateContract items that have a matching Rate earlier in the list. Correct? Commented May 16, 2019 at 6:22
  • If you see I have Overridden the equals method in the Rate class.. So two rates are same or not will be defined by that equals method Commented May 16, 2019 at 6:24
  • I need some help implementing java 8 in filterDuplicateRatesInSameContracts method Commented May 16, 2019 at 6:25

1 Answer 1

0

I'm interpreting your question as 'how do I move to a separate list all contracts in a list that have the same rate as an earlier item in the list'?

If so:

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);
    }
}

Note that this uses Iterator so that you can remove the contracts with matching rates as you move through the list. An alternative would be to collect them in the duplicates list and then remove them afterwards. Both would work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.