0

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

Khushi
  • 325
  • 1
  • 11
  • 32
  • 1
    show us some code please? – Jude Niroshan Sep 02 '16 at 03:00
  • @Jude I have added some code snippet.. – Khushi Sep 02 '16 at 05:25
  • First, replace your use of the troublesome old `java.util.Date` class with the modern java.time classes, probably [`Instant`](http://docs.oracle.com/javase/8/docs/api/java/time/Instant.html) in your case. – Basil Bourque Sep 02 '16 at 18:12
  • @Basil , I cant map LocalDate to JPA equivalent type? – Khushi Sep 05 '16 at 02:04
  • Looks like in [JPA 2.1](https://en.wikipedia.org/wiki/Java_Persistence_API#JPA_2.1) you can make use of fairly simple JPA 2.1 Type Converters for java.time types. [Oracle blog](https://blogs.oracle.com/theaquarium/entry/using_the_java_8_date). [JPA spec issue](https://java.net/jira/browse/JPA_SPEC-63). [Stack Overflow](http://stackoverflow.com/questions/37166291/jpa-2-1-introducing-java-8-date-time-api). – Basil Bourque Sep 05 '16 at 02:31
  • Your Question is not clear to me. From the lack of response, I suppose others too are confused. I suggest you edit your Question to better explain. Try to focus as narrowly as possible on your core issue and remove extraneous matters. – Basil Bourque Sep 05 '16 at 02:34

0 Answers0