RFC: draft API for JEP 269 Convenience Collection Factories (original) (raw)
Paul Sandoz paul.sandoz at oracle.com
Thu Oct 15 14:40:18 UTC 2015
- Previous message: RFC: draft API for JEP 269 Convenience Collection Factories
- Next message: RFC: draft API for JEP 269 Convenience Collection Factories
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 15 Oct 2015, at 16:28, Stephen Colebourne <scolebourne at joda.org> wrote:
I've been working on a Java 8 wrapper class around double[] in my day job, and added the following factory method: /** * Obtains an instance with entries filled using a function. * * The function is passed the array index and returns the value for that index. * * @param size the number of elements * @param valueFunction the function used to populate the value * @return an array initialized using the function */ public static DoubleMatrix1D of(int size, IntToDoubleFunction valueFunction) { if (size == 0) { return EMPTY; } double[] array = new double[size]; for (int i = 0; i < array.length; i++) { array[i] = valueFunction.applyAsDouble(i); } return new DoubleMatrix1D(array); }
Within that method you can use Arrays.setAll(array, valueFunction). In hindsight it would have been useful for that method to return the filled array.
Paul.
- Previous message: RFC: draft API for JEP 269 Convenience Collection Factories
- Next message: RFC: draft API for JEP 269 Convenience Collection Factories
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]