title: Instantiating array of objects with streams
body: <p>I have an array of ints that I want to use to instantiate a array or list of objects.  In my case, the old-fashoned way to do it would be:</p>
  <pre><code>int[] layer_sizes = {784, 500, 10};
  Layer[] layers = new Layer[layer_sizes.length];
  for (int i=0; i&lt;layer_sizes.length; i++)
      layers[i] = new Layer(layer_sizes[i]);
  </code></pre>
  <p>But now I see Java 8 has all these fancy streams.  I now want to do something like Python's list comprehensions:</p>
  <pre><code>List&lt;Layer&gt; layers = Stream.of(layer_sizes).map(size -&gt; Layer(size));
  </code></pre>
  <p>But it doesn't let me do that, and I'm not sure why... The message it gives is</p>
  <pre><code>incompatible types: no instance(s) of type variable(s) R exist so that Stream&lt;R&gt; comforms to List&lt;Layer&gt; where R, T are type variables....
  </code></pre>
  <p>Is there a way to use Streams to construct an array of objects in one line?</p>
  <p>EDIT: Not a duplicate of previous question, because it turns out that there're some peculiarities of making streams from primitives.</p>
  <h1>Conclusion</h1>
  <p>Thank you Sam Sun and Eran.  The line I ended up using was this:</p>
  <pre><code>Layer[] layers = Arrays.stream(layer_sizes).boxed().map(Layer::new).toArray(Layer[]::new);
  </code></pre>
  <p>Whatever boxed() is, you need it, unless you declare layer_sizes as an <code>Integer</code> instead of <code>int</code>.</p>
  <p><strong>P.S.</strong> If the java developers are reading this, it would be amazing for Java 9 or whatever's next to have something like</p>
  <pre><code>Layer[] layers = {new Layer(size) for (size:layer_sizes)}  // OR at least:
  Layer[] layers = Stream.of(layer_sizes).map(Layer::new).toArray()
  </code></pre>