0

I Have this method that displays an array of objects in a document.

public File generateODSFileWithTemplate(String fileName, Object[][] lignes, File template) throws FileNotFoundException, IOException, JDOMException, TemplateException {

    final Sheet sheet = SpreadSheet.createFromFile(template).getSheet(0);
    sheet.setRowCount(lignes.length);
    
    int column = 0;
    int row = 0;
    //Iterating through the array of Object
    for(Object[] rowObj : lignes){
        for(Object colObj : rowObj){
            sheet.setValueAt(rowObj[column],column,row );
            column++;
        }
        row++;
        column = 0;
    }
    File outFile = new File(fileName);
    sheet.getSpreadSheet().saveAs(outFile);

    return outFile;
}

Is there a way to use streams instead of for loop?

2
  • 1
    It's inadvisable since you would be using streams to manipulate an existing object outside of the streaming process (which under certain circumstances can have undesirable side effects). And the column increment could create effective final problems. Now I'm not saying it's not possible, but there is nothing wrong with the way you are doing it (assuming it achieves the desired result of course). Commented Mar 29, 2021 at 22:08
  • 1
    Also, streams should tend to make the process more clearly reflect the problem statement. Like grouping items in a map. I think a stream solution would add unnecessary complexity and tend to muddy the waters so to speak. Commented Mar 29, 2021 at 22:22

1 Answer 1

4

I don't think using Stream API is appropriate in this particular case. Although you could still improve your for loops by using plain old loops instead of the enhanced ones:

for(int row = 0; row < lines.length; row++) {
    for(int col = 0; col < lines[row].length; col++){
        sheet.setValueAt(lines[row][col], col, row);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.