title: Access element of previous step in a stream or pass down element as "parameter" to next step?
body: <p>What is a good way to access an element of a previous step of a stream?</p>
  
  <p>The example here starts with a stream of parents (String) and flatMaps them to two children (Integer) for each parent. At the step where I am dealing with the Integer children (forEach) I would like to "remember" what the parent String was ('parentFromPreviousStep' compilation error).</p>
  
  <p>The obvious solution is to introduce a holder class for a String plus an Integer and in the flatMap produce a stream of such holder objects. But this seems a bit clumsy and I was wondering whether there is a way to access a "previous element" in such a chain or to "pass down a parameter" (without introducing a special holder class).</p>
  
  <pre><code>    List&lt;String&gt; parents = new ArrayList();
      parents.add("parentA");
      parents.add("parentB");
      parents.add("parentC");
  
      parents.stream()
              .flatMap(parent -&gt; {
                  List&lt;Integer&gt; children = new ArrayList&lt;&gt;();
                  children.add(1);
                  children.add(2);
                  return children.stream();
              }).forEach(child -&gt; System.out.println(child + ", my parent is: ???" + parentFromPreviousStep));
  </code></pre>
  
  <p><em>Background:</em> This actually happens in Nashorn with the idea to provide JavaScript scripting code for an application without having to deploy the Java application. This is why I do not want to introduce a special Java holder class for my two business objects (which are simplified here as String and Integer). [The Java code above can be written almost the same in Nashorn, and the stream functions are Java, not JavaScript.]</p>