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