To fill a List
, it is possible to generate an infinite Stream
using Stream.generate(s)
and then limit the number of results with limit(maxSize)
.
For example, to fill a List
of 10 new ScheduleIntervalContainer
objects:
List<ScheduleIntervalContainer> scheduleIntervalContainers =
Stream.generate(ScheduleIntervalContainer::new).limit(10).collect(toList());
The generate
method takes a Supplier
: in this case, the supplier is a method reference creating new instance of ScheduleIntervalContainer
each time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…