我有一个列表,其中有一些对象是从Excel文件中检索的。 这些对象是文件的行,由行定义: F_Name (名字), L_Name (姓氏), data_nascita (即出生日期)和 data_sinistro (即车祸发生日期)。
Excel文件用于模拟从数据库(具有更多字段)中提取查询,该数据库从前三个字段中唯一识别人员: F_Name , L_Name , data_nascita 。
文件中有8行; 前四行具有与后四行中的前三个字段相同的前三个字段; 该列表由这8行填充。
我的需要是: 如果在Excel文件中有一行,其前三个字段与另一行相同,则该列表必须仅包含具有最后一个日期的第四个字段( data_sinistro )的行。
这里有我的类(1 =应用所描述逻辑的ReadExcelDemo),(2 =数据,包含Excel文件四个字段的setter和getter)。
我试图对字段列表进行排序或比较,但没有成功...
请帮助我,谢谢大家!
(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;
}
}
答案 0 :(得分:1)
如果您使用的是Java 8或更高版本,请尝试此操作。我认为它将解决。
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;
}