title: Nested parallel stream execution in Java - findAny() randomly fails
body: <p>The following code throws the <em>IllegalArgumentException</em> in every 10-15 try for the <strong>same</strong> input:</p>
  <pre><code>        AllDirectedPaths&lt;Vertex, Edge&gt; allDirectedPaths = new AllDirectedPaths&lt;&gt;(graph);
          List&lt;GraphPath&lt;Vertex, Edge&gt;&gt; paths = allDirectedPaths.getAllPaths(entry, exit, true, null);
  
  
          return paths.parallelStream().map(path -&gt; path.getEdgeList().parallelStream()
                  .map(edge -&gt; {
                      Vertex source = edge.getSource();
                      Vertex target = edge.getTarget();
  
                      if (source.containsInstruction(method, instructionIndex)) {
                          return source;
                      } else if (target.containsInstruction(method, instructionIndex)) {
                          return target;
                      } else {
                          return null;
                      }
                  }).filter(Objects::nonNull)).findAny().flatMap(Stream::findAny)
                  .orElseThrow(() -&gt; new IllegalArgumentException(&quot;Given trace refers to no vertex in graph!&quot;));
  </code></pre>
  <p>The idea of the code is to find a vertex that wraps a certain instruction (see <code>containsInstruction()</code>), whereas the vertex is on at least one path from the <code>entry</code> to the <code>exit</code> vertex. I'm aware that the code is not optimal in terms of performance (every intermediate vertex on a path is looked up twice), but that doesn't matter.</p>
  <p>The input is simply a trace (String) from which the <code>method</code> and <code>instructionIndex</code> can be derived. All other variables are <em>fixed</em> in that sense. Moreover, the method <code>containsInstruction()</code> doesn't have any side effects.</p>
  <p>Does it matter where to put the 'findAny()' stream operation? Should I place it directly following the filter operation? Or are nested parallel streams the problem?</p>