title: ArrayStoreException when trying to collect files into array via Files.walk
body: <p>I am trying to understand the Java 8 Stream-feature.</p>
  
  <p>I would like to catch all files in an array (could als be list) in all subdirectories of a specific directory path.</p>
  
  <p>My code throws an java.lang.ArrayStoreException Error</p>
  
  <blockquote>
  <pre><code>  File[] files = Files.walk(Paths.get(path))
        .filter(Files::isRegularFile)
        .toArray(File[]::new);
  </code></pre>
  </blockquote>
  
  <p>Another thing I tried is just add the file names to an existing ArrayList:</p>
  
  <blockquote>
  <pre><code>  ArrayList&lt;String&gt; existingNames = new ArrayList&lt;String&gt;();
    Files.walk(Paths.get(path))
        .filter(Files::isRegularFile)
        .forEach(p -&gt; existingNames.add(p.getFileName()));
  </code></pre>
  </blockquote>
  
  <p>This throws a java.lang.ArrayStoreException Error as well.</p>
  
  <p>Something else I tried with the same result was:</p>
  
  <blockquote>
  <pre><code>  File[] files = Files.walk(Paths.get(path))
        .filter(Files::isRegularFile)
        .toArray(File[]::new);
  </code></pre>
  </blockquote>
  
  <p>Can anyone point me in the right direction? </p>
  
  <p>The full stack trace is:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ArrayStoreException  at
    java.lang.System.arraycopy(Native Method)     at
    java.util.stream.SpinedBuffer.copyInto(SpinedBuffer.java:198)     at
    java.util.stream.Nodes$SpinedNodeBuilder.copyInto(Nodes.java:1290)    at
    java.util.stream.SpinedBuffer.asArray(SpinedBuffer.java:215)  at
    java.util.stream.Nodes$SpinedNodeBuilder.asArray(Nodes.java:1296)     at
    java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:439)
      at
    nachbearbeitung.CheckExistingMAIDs.main(CheckExistingMAIDs.java:41)</p>
  </blockquote>
  
  <p>Thanks in advance!</p>