簡體   English   中英

從另一個迭代和另一個類添加到HashSet

[英]Adding to a HashSet from another iteration and another class

我正在使用Apache POI從電子表格的工作表1中填充3個值的HashSet。 由於我還需要訪問電子表格的表2以獲得另一個值,因此我再次對其進行遍歷:

public class Students {

private int numStudents;
HashSet<Student> studentsRoster1 = new HashSet<Student>();
HashSet<Student> studentsRoster;

public Students(String studentsDb) {

    try {
        FileInputStream file = new FileInputStream(new File(studentsDb));

        XSSFWorkbook workbook = new XSSFWorkbook(file);

        DataFormatter fmt = new DataFormatter();

        // Get first sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        String name = null;
        String email = null;
        int id1 = 0;
        String id = null;

        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Student student = new Student();

            Row row = rowIterator.next();
            // Skip the first row
            if (row.getRowNum() == 0) {
                continue;
            }

            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();

                Cell cell0 = row.getCell(0);
                Cell cell1 = row.getCell(1);
                String formatValue = fmt.formatCellValue(cell1);
                Cell cell2 = row.getCell(2);

                name = cell0.getStringCellValue();
                id1 = (int) cell1.getNumericCellValue();
                email = cell2.getStringCellValue();

                id = String.valueOf(id1);

                student.setName(name);
                student.setid(id);
                student.setEmail(email);

                studentsRoster1.add(student);
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    /**** access second sheet for team info ****/
    try {
        FileInputStream file = new FileInputStream(new File(studentsDb));

        XSSFWorkbook workbook = new XSSFWorkbook(file);

        // Get second sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(1);

        String team = null;

        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Student student = new Student();

            Row row = rowIterator.next();
            // Skip the first row
            if (row.getRowNum() == 0) {
                continue;
            }

            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();

                Cell cell0 = row.getCell(0);

                team = cell0.getStringCellValue();

                student.setTeam(team);
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    this.studentsRoster = studentsRoster1;
}
public HashSet<Student> getStudents() {
    return studentsRoster;
}
}

如您所見,我正在創建HashSet studentsRoster1; 但最后,我需要歸還學生羅斯特。 我還沒有找到一種方法來添加第4個值的student.setTeam(team); 正確的HashSet。 我是否要創建另一個HashSet並使用並集?

我還需要從另一個類向studentsRoster HashSet中添加內容,該類正在迭代另一個電子表格以添加另一個值student.setXXX(xxx);

我也無法做到這一點。

任何建議深表感謝。

您可以在一個循環中完成此操作,如下所示:

private static final int NAME_INDEX = 0;
private static final int ID_INDEX = 1;
private static final int EMAIL_INDEX = 2;
private static final int TEAM_INDEX = 0;

private int numStudents;
HashSet<Student> studentsRoster = new HashSet<Student>();


public Students(String studentsDb) {
    try {
        HashSet<Student> newStudentsRoster = new HashSet<Student>();
        FileInputStream file = new FileInputStream(new File(studentsDb));

        XSSFWorkbook workbook = new XSSFWorkbook(file);

        // Get first sheet from the workbook
        XSSFSheet sheet0 = workbook.getSheetAt(0);

        // Get second sheet from the workbook
        XSSFSheet sheet1 = workbook.getSheetAt(1);

        Iterator<Row> rowIterator0 = sheet0.iterator();
        Iterator<Row> rowIterator1 = sheet1.iterator();
        while (rowIterator0.hasNext() && rowIterator1.hasNext()) {
            Row row0 = rowIterator0.next();
            Row row1 = rowIterator1.next();
            // Skip the first row
            if (row0.getRowNum() > 0) {
                Student student = new Student();
                Iterator<Cell> cellIterator0 = row0.cellIterator();
                Iterator<Cell> cellIterator1 = row1.cellIterator();
                if (cellIterator0.hasNext()) {
                    student.setName(row0.getCell(NAME_INDEX).getStringCellValue());
                    Integer id = row0.getCell(ID_INDEX).getNumericCellValue();
                    if (id != null){               
                        student.setId(id.toString());
                    }
                    student.setEmail(row0.getCell(EMAIL_INDEX).getStringCellValue());
                }
                if (cellIterator1.hasNext()) {
                    student.setTeam(row1.getCell(TEAM_INDEX).getStringCellValue());
                }
                newStudentsRoster.add(student);
            }
        }
        numStudents = newStudentsRoster.size();
        studentsRoster = newStudentsRoster;
    } catch (FileNotFoundException e) {
        e.printStackTrace(); // <- this hides the errors, you must avoid it
    } catch (IOException e) {
        e.printStackTrace(); // <- this hides the errors, you must avoid it
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM