根据特定值

时间:2018-04-13 04:05:57

标签: java list arraylist linked-list apache-poi

我有一个包含大量值的excel文件,即ID, fullname, Date/Time and state,并且基于Date/Time and state的值,对于具有相同ID and fullname的用户,有多个条目。

但我只需要那些日期/时间最小且最大存储到列表中的条目,例如它的字段为ID, fullname, Max and Min

到目前为止,我已经为最大值和最小值创建了单独的列表。 我的代码如下所示。我正在使用java。

public class ExcelFileMigration {
private static HashMap<String,AttendanceMigrate> attendanceEntrySet =new HashMap<>();
private static HashMap<String,AttendanceMigrate> attendanceExitSet =new HashMap<>();
private static List<AttendanceMigrate> attendanceFinalList =new ArrayList<>();

public static void main(String[] args) throws IOException, ClassNotFoundException {
    // TODO Auto-generated method stub
    System.out.println("Started exec");
    saveToDB();

}
public static String saveToDB() throws IOException {
    // TODO Auto-generated method stub
    Properties properties = new Properties();
    InputStream input = null;
    System.out.println("Inside DB save");
    input = new FileInputStream("/home/workspace/Attendance.properties");
    properties.load(input);
    FileInputStream stream = new FileInputStream(properties.getProperty("url"));
    String attendanceDate = properties.getProperty("dateVismaya");
    System.out.println("Stream : "+stream);
    Map<String, Integer> areaMap=new HashMap<String, Integer>();
    AttendanceMigrate attendanceMigrate  = null;
    HSSFWorkbook wb = new HSSFWorkbook(stream);
    HSSFSheet sheet = wb.getSheetAt(0);
    Row title = sheet.getRow(5);
    List<String>sheetTitles =new ArrayList<String>();
    for(int c=1; c<7 ; c++){
        try{

            if(title.getCell(c).getStringCellValue().equals("ID"))

            {
                sheetTitles.add(title.getCell(c).getStringCellValue());
            }

            if(title.getCell(c).getStringCellValue().equals("Full Name"))

            {
                sheetTitles.add(title.getCell(c).getStringCellValue());
            }

            if(title.getCell(c).getStringCellValue().equals("Date/Time"))

            {

                sheetTitles.add(title.getCell(c).getStringCellValue());
            }
            if(title.getCell(c).getStringCellValue().equals("State"))

            {
                sheetTitles.add(title.getCell(c).getStringCellValue());
            }


        }catch(NullPointerException e){
            e.printStackTrace();
        }
    }

    int countEmp = 1;
    int numberOfRows = sheet.getPhysicalNumberOfRows();
    System.out.println("Number of rows : "+numberOfRows);
    if(numberOfRows >1){
        for(int j=6; j < numberOfRows; j++){
             countEmp = 1;
            Row row = sheet.getRow(j);
            attendanceMigrate=new AttendanceMigrate();
            for (int i = 1; i < 5; i++) {
                Cell cell = row.getCell(i);
                String column = (title.getCell(i)).getStringCellValue();
                if(column.equalsIgnoreCase("ID")){
                    attendanceMigrate.setEmpCode(cell.getStringCellValue());
                }
                if (column.equalsIgnoreCase("Full Name")) {
                    attendanceMigrate.setUserName(cell.getStringCellValue());
                }
                if(column.equalsIgnoreCase("Date/Time")){
                    attendanceMigrate.setDateTime(cell.getStringCellValue());
                }
                if (column.equalsIgnoreCase("State")) {
                    attendanceMigrate.setState(cell.getStringCellValue());
                }

            }

            List empCodes=new ArrayList<>();
            empCodes.add("1");
            empCodes.add("2");
            empCodes.add("33");
            empCodes.add("70");
            empCodes.add("146");
            empCodes.add("502");
            if(empCodes.contains(attendanceMigrate.getEmpCode())){
                countEmp = 0;
            }
            if ((!attendanceMigrate.getUserName().isEmpty()) && (countEmp != 0)) {
                if(!attendanceEntrySet.containsKey(attendanceMigrate.getEmpCode()))
                    attendanceEntrySet.put(attendanceMigrate.getEmpCode(),attendanceMigrate);
                attendanceExitSet.put(attendanceMigrate.getEmpCode(),attendanceMigrate);
            }       


        }
    }
    Iterator it = attendanceEntrySet.entrySet().iterator();
    Iterator it1 = attendanceExitSet.entrySet().iterator();

    while(it.hasNext()){
        //System.out.println(attendanceEntrySetVimsaya.get(z));
        Map.Entry pair = (Map.Entry)it.next();
        Map.Entry pair1 = (Map.Entry)it1.next();
        System.out.println("Entry" +" = " + pair.getValue());
        System.out.println("Exit" + " = " + pair1.getValue());
    }

    return null;
}
}

AttendanceMigrate类如下:

public class AttendanceMigrate implements Serializable{


    String userName;
    String empCode;
    String dateTime;
    String state;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getEmpCode() {
        return empCode;
    }
    public void setEmpCode(String empCode) {
        this.empCode = empCode;
    }
    public String getDateTime() {
        return dateTime;
    }
    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    @Override
    public String toString() {
        return "AttendanceMigrate [userName=" + userName + ", empCode=" + empCode + ", dateTime=" + dateTime
                + ", state=" + state + "]";
    }

0 个答案:

没有答案