Zakaria Marrah
Zakaria Marrah

Reputation: 765

Iterating an array of objects using streams

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?

Upvotes: 0

Views: 460

Answers (1)

ETO
ETO

Reputation: 7299

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);
    }
}

Upvotes: 4

Related Questions