【发布时间】:2017-02-16 22:09:45
【问题描述】:
我们如何使用 Java Streams 方法来收集在for 循环中生成的对象?
例如,这里我们通过重复调用YearMonth::atDay 为YearMonth 表示的一个月中的每一天生成一个LocalDate 对象。
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 );
}
这可以用流重写吗?
【问题讨论】:
标签: java for-loop collections java-stream