0

I have a list and within it there are some objects that are retrieved from an Excel file. These objects are the rows of the file, defined by the colums: F_Name (First name), L_Name (Last name), data_nascita (that is Date of birth) and data_sinistro (that is Date of car accident).

The Excel file is used to simulate a query extraction from a db (that has many more fields) where the people are identified univocally from the first three fields: F_Name, L_Name, data_nascita.

There are 8 rows in the file; the first four rows, has the first three fields that are identically to the first three fields in the last 4 rows; the list is filled by these 8 rows.

My need is: If in the Excel file there is a row that has the first three fields identically to another row, the list must include only the row that has the fourth field (data_sinistro) with the last date.

Here there are my classes (1 = ReadExcelDemo that applicates the logic described), (2 = Datas, contains setter and getters for the four fields of the Excel file).

I tried to sort or compare the field's list but without success...

Please help me, thank you guys!

(1)

package com.testing;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ReadExcelDemo {

    public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
        String path = "C:\\Users\\CorsoUser08\\Documents\\DXC Technology\\tabellaAssicuratoEstrazioneJsonSigim.xlsx";
        ReadExcelDemo(path);
    }

    public static void ReadExcelDemo(String path) throws EncryptedDocumentException, InvalidFormatException, IOException {
           File file = new File(path);

           Workbook workbook = WorkbookFactory.create(file);
           Sheet sheet = workbook.getSheetAt(0);

           Iterator<Row> rowIterator = sheet.rowIterator();

           checkRows(rowIterator);
       }

    private static void checkRows(Iterator<Row> rowIterator) {
        // TODO Auto-generated method stub
        ArrayList<Datas> list = new ArrayList<Datas>();

        Datas obj = new Datas();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            for (int i = 0; i < 4; i++) {
                obj.setF_Name(row.getCell(0).toString());
                obj.setL_Name(row.getCell(1).toString());
                obj.setData_nascita(row.getCell(2).toString());
                obj.setData_sinistro(row.getCell(3).toString());
            }
            list.add(obj); 
            System.out.println(obj.toString());
       }

//Start of comment//
            /*
            list.sort(new Comparator<Datas>() {

                public int compare(Datas o1, Datas o2) {
                    // TODO Auto-generated method stub
                    if (o1.getData_sinistro().compareTo(o2.getData_sinistro()) >= 0) {

                        return 0;
                    }
                    return 0;
                }       
            });

        Iterator<Datas> datasList = list.iterator();
        while (datasList.hasNext()) {
            Datas dat = datasList.next();
            System.out.println(dat.getF_Name());
            System.out.println(dat.getL_Name());
            System.out.println(dat.getData_nascita());
            System.out.println(dat.getData_sinistro());
        }
        */  
//End of comment//

    } 

}

(2)

package com.testing;

import java.util.Date;

public class Datas {

        String data_sinistro;
        String F_Name;
        String L_Name;

        String data_nascita;
        public String getData_nascita() {
            return data_nascita;
        }
        public void setData_nascita(String data_nascita) {
            this.data_nascita = data_nascita;
        }
        public String getData_sinistro() {
            return data_sinistro;
        }
        public void setData_sinistro(String data_sinistro) {
            this.data_sinistro = data_sinistro;
        }
        public String getF_Name() {
            return F_Name;
        }
        public void setF_Name(String f_Name) {
            F_Name = f_Name;
        }
        public String getL_Name() {
            return L_Name;
        }
        public void setL_Name(String l_Name) {
            L_Name = l_Name;
        }
}

  • The issue with the code is that you are returning zero in either cases while using comparator. In either cases all are being treated as the same. – beginner_coder Dec 06 '19 at 11:52
  • 2
    What is the `for (int i = 0; i < 4; i++) { ... }` for? Why do you think the code in this for loop needs to be executed four times? – Axel Richter Dec 06 '19 at 12:06
  • The for loop is executed 4 times because there are 4 columns in the Excel file – The Big Gamer Dec 06 '19 at 16:14
  • In the `for (int i = 0; i < 4; i++) ` loop: `row.getCell(0)` gets the cell in first column of that row, `row.getCell(1)` gets the cell in second column, `row.getCell(2)` gets the cell in third column and `row.getCell(3)` gets the cell in fourth column. And all this four times. Why? Hint: If the loop variable `i` is never used within the loop, then the loop only executes the same things multiple times. – Axel Richter Dec 06 '19 at 17:54
  • Does this answer your question? [Remove duplicates from a list of objects based on property in Java 8](https://stackoverflow.com/questions/29670116/remove-duplicates-from-a-list-of-objects-based-on-property-in-java-8) – Noa Jan 14 '20 at 14:48

1 Answers1

1

If you are using java 8 or higher, try this. I think it will solve.

private static ArrayList<Datas> checkRows(Iterator<Row> rowIterator) {
        // TODO Auto-generated method stub
        ArrayList<Datas> list = new ArrayList<Datas>();


        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            Datas obj = new Datas();
            obj.setF_Name(row.getCell(0).toString());
            obj.setL_Name(row.getCell(1).toString());
            obj.setData_nascita(row.getCell(2).toString());
            obj.setData_sinistro(row.getCell(3).toString());

            Optional<Datas> opt = list.stream().filter(data -> {
                return data.getF_Name().equals(obj.getF_Name()) && data.getL_Name().equals(obj.getL_Name())
                        && data.getData_nascita().equals(obj.getData_nascita());
            }).findFirst();

            if (opt.isPresent()) {
                Datas inList = opt.get();
                if (inList.getData_sinistro().compareTo(obj.data_sinistro) < 0) {
                    inList.setData_sinistro(obj.data_sinistro);
                }
            } else {
                list.add(obj);
            }

       }

       return list;

    } 
Guilherme Melo
  • 209
  • 3
  • 13
  • 1
    @Guilherme: I would give you (+1) if you would remove the superfluous `for (int i = 0; i < 4; i++)`. @The Big Gamer: This code does exactly what you have described. You have seen, that `Datas obj = new Datas();` was shifted into the `while (rowIterator.hasNext())` loop? Because as you had done, the `list` only contains the same object `obj` multiple times . – Axel Richter Dec 07 '19 at 06:29
  • Thanks, I didnt see that this For was unesessary. – Guilherme Melo Dec 08 '19 at 19:54