No, no, and any equivalent construct (which simply stores int n and the provider and calls the provider for each get ) seems like a terrible idea. However, apparently, you just want to read n objects from Supplier and save them in a list. In this case, Sean's answer is probably the best.
Just for fun, but here you can create an ImmutableList size n by calling Supplier n times ( transform , limit and cycle all of Iterables ):
public static <T> ImmutableList<T> nInstances(int n, Supplier<T> supplier) { return ImmutableList.copyOf(transform( limit(cycle(supplier), n), Suppliers.<T>supplierFunction())); }
I uh ... would not recommend this compared to a simple loop implementation (for greater readability).
source share