OnJava8-Examples/onjava/CollectionData.java
Bruce Eckel bc0026c3e1 Radically reorganized
Plus many new examples in "streams" chapter.
2015-11-11 20:20:04 -08:00

18 lines
497 B
Java

// onjava/CollectionData.java
// A Collection filled with data using a generator object.
package onjava;
import java.util.*;
import java.util.function.*;
public class CollectionData<T> extends ArrayList<T> {
public CollectionData(Supplier<T> gen, int quantity) {
for(int i = 0; i < quantity; i++)
add(gen.get());
}
// A generic convenience method:
public static <T> CollectionData<T>
list(Supplier<T> gen, int quantity) {
return new CollectionData<>(gen, quantity);
}
}