Converting loop to Java 8 streams
问题 How can I convert the following looping code to simple Java 8 streams? List<String> headers = new ArrayList<>(); ... int column = 0; for(String text:headers){ Cell cell = header.createCell(column++); cell.setCellValue(text); } 回答1: Streams won't be needed. Use an AtomicInteger and Iterable#forEach: AtomicInteger column = new AtomicInteger(0); headers.forEach(text -> header.createCell(column.getAndIncrement()).setCellValue(text)); Whether that's more readable is up to you. 来源: https:/