title: How to create 2D (two dimensional) array from Stream of Stream in Java?
body: <p>I have an <code>Iterator</code> of <code>Iterator</code>, from which I want to generate a 2D array. I have the following code:</p>
  
  <pre><code>StreamSupport.stream(Spliterators.spliteratorUnknownSize(rowIterator, Spliterator.ORDERED), false)
          .map((Row row) -&gt; {
              Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
              return StreamSupport.stream(Spliterators.spliteratorUnknownSize(cellIterator, Spliterator.ORDERED), false)
                      .map(Utils::genericCellMapper)
                      .toArray();
          })
          .toArray(Object[][]::new);
  </code></pre>
  
  <p>But Eclipse editor is giving compilation error: <code>Cannot infer type argument(s) for &lt;R&gt; map(Function&lt;? super T,? extends R&gt;)</code>.</p>
  
  <p>If I change the above code as:</p>
  
  <pre><code>StreamSupport.stream(Spliterators.spliteratorUnknownSize(rowIterator, Spliterator.ORDERED), false)
          .map(new Function&lt;Row, Object[]&gt;() {
  
              @Override
              public Object[] apply(Row row) {
                  Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
                  List&lt;Object&gt; list = StreamSupport.stream(Spliterators.spliteratorUnknownSize(cellIterator, Spliterator.ORDERED), false)
                          .map(Utils::genericCellMapper)
                          .collect(Collectors.&lt;Object&gt;toList());
  
                  return list.toArray(new Object[list.size()]);
              }
          })
          .toArray(Object[][]::new);
  </code></pre>
  
  <p>It works. What I understand that in the first variant compiler is unable to determine the return type of the mapper. Do I need to use the second variant to achieve the requirement or it is a problem with Eclipse?</p>
  
  <hr>
  
  <p>Similar question from other SO threads</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/23079003/how-to-convert-a-java-8-stream-to-an-array">How to Convert a Java 8 Stream to an Array?</a></li>
  <li><a href="https://stackoverflow.com/questions/30817432/how-to-create-a-two-dimensional-array-from-a-stream-in-java-8">How to create a two dimensional array from a stream in Java 8?
  </a></li>
  </ul>