I am having 1 list of custom objects (around 20,000 or 30,000 records), -> I need to compare its records to subsequent records based on 3 parameters(type, name and country )and -> if any records find equal, i have to check date for these two records -> and only have to keep record with recent date as validated status and marking other record with old status.
Note- max one record can be equal to one record / at a time not 3 or more records can be equal
Right now, I am using ArrayList , implemented equals method in domain class based on 3 parameter. Checking each record to other records and if found equal -> doing date check and marking their status accordingly and breaking from look and continuing with other records -> At the end removing all d records with status 'Old'
Can we achieve it in java 8 or other APIs like Apache commons, in effective way?
Code snippet-
`public class Domain implements Comparable<Domain> {
private String type;
private String name;
private String country;
private Date vaildDate;
private String staus;
getter setter
equals method based on type, name , country
public int compareTo(Domain domain) {
return (this.vaildDate).compareTo(domain.getVaildDate());
}
}
In Spring service class
public void saveValidatedRecords() throws IOException {
List<Domain> validatedRecordsInFile = processExternalFTPFile();
List<Domain> dbRecords = dao.findRec(Integer id);
//i have to compare db rec and file records and process according to my question , i am merging both list and processing
List<Domain> listToBeSaved = new ArrayList<Domain>(dbRecords.size()+validatedRecordsInFile.size());
listToBeSaved.addAll(validatedRecordsInFile);
listToBeSaved.addAll(dbRecords);
for (int i= 0; i< listToBeSaved.size(); i++) {
for (int j= i+1;j< listToBeSaved.size() ;j++) {
if (listToBeSaved.get(i).equals(listToBeSaved.get(j)) ) {
if(listToBeSaved.get(i).getValidDate().after(listToBeSaved.get(j).getValidDate()) {
listToBeSaved.get(i).getStatus("Valid");
listToBeSaved.get(j).getStatus("Old");
break; //since only one record will be equal and control goes to outer for
}
}
}
}`
I can use 2 list also here, no need to merge.
Thanks