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