How can we use the Java Streams approach to collecting objects generated in a for
loop?
For example, here we generate one LocalDate
object for each day in a month represented by YearMonth
by repeatedly calling YearMonth::atDay
.
YearMonth ym = YearMonth.of( 2017 , Month.AUGUST ) ;
List<LocalDate> dates = new ArrayList<>( ym.lengthOfMonth() );
for ( int i = 1 ; i <= ym.lengthOfMonth () ; i ++ ) {
LocalDate localDate = ym.atDay ( i );
dates.add( localDate );
}
Can this be rewritten using streams?