简体   繁体   English

从另一个迭代和另一个类添加到HashSet

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

I am using Apache POI to populate a HashSet with 3 values from sheet 1 of a spreadsheet. 我正在使用Apache POI从电子表格的工作表1中填充3个值的HashSet。 Since I also need to access sheet 2 of the spreadsheet for another value, I'm iterating through it again: 由于我还需要访问电子表格的表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;
}
}

As you can see, I am creating HashSet studentsRoster1; 如您所见,我正在创建HashSet studentsRoster1; but in the end, I need to return studentsRoster. 但最后,我需要归还学生罗斯特。 I have not figured out a way to add that 4th value student.setTeam(team); 我还没有找到一种方法来添加第4个值的student.setTeam(team); to the HashSet correctly. 正确的HashSet。 Do I want to create another HashSet and use union? 我是否要创建另一个HashSet并使用并集?

I also need to add to the studentsRoster HashSet from another class, which is iterating another spreadsheet to add another value, student.setXXX(xxx); 我还需要从另一个类向studentsRoster HashSet中添加内容,该类正在迭代另一个电子表格以添加另一个值student.setXXX(xxx); .

I'm unable to do that either. 我也无法做到这一点。

Any advice is much appreciated. 任何建议深表感谢。

You can do that in a single loop, like this: 您可以在一个循环中完成此操作,如下所示:

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