0

Need ideas for converting below loop into streams. I am new to java 8 streams and learning practically how it will implemented.

for (int i=0; i < headerNames.size(); i++){
    XSSFCell cell = headerRow.createCell(i);
    cell.setCellValue(headerNames.get(i));
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • you create a cell, set its value, and lose it, that's it with the code? – Naman Dec 09 '20 at 08:14
  • 1
    @Naman I *assume* ... the newly created cell belongs to the headerRow. So it is *not* lost. – GhostCat Dec 09 '20 at 08:17
  • Why do you want to convert this to a stream? Not everything should be a stream. Just because streams are a new tool in your toolbox, doesn't mean you should use it for everything. – Mark Rotteveel Dec 09 '20 at 08:47

1 Answers1

0

You can try the below snippet,

IntStream.range(0, headerNames.size())
         .forEach(i -> headerRow.createCell(i)
                                .setCellValue(headerNames.get(i)));

Stream indices in headerNames and for each index create a cell and set its value.

Prasanna
  • 2,390
  • 10
  • 11