RFC: draft API for JEP 269 Convenience Collection Factories

Paul Sandoz paul.sandoz at oracle.com
Thu Oct 15 14:40:18 UTC 2015


> 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.
>   * <p>
>   * 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.



More information about the core-libs-dev mailing list