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

19 lines
515 B
Java

// onjava/Generated.java
package onjava;
import java.util.function.*;
public class Generated {
// Fill an existing array:
public static <T> T[] array(T[] a, Supplier<T> gen) {
return new CollectionData<>(gen, a.length).toArray(a);
}
// Create a new array:
@SuppressWarnings("unchecked")
public static <T> T[] array(Class<T> type,
Supplier<T> gen, int size) {
T[] a =
(T[])java.lang.reflect.Array.newInstance(type, size);
return new CollectionData<>(gen, size).toArray(a);
}
}