title: How to propagate variables in a stream in java 8
body: <p>I would be currious to know how to propagate variable into a stream in java 8.
  An example is better than a long explaination, so how would you convert the following (abstract) code into streams:</p>
  
  <pre><code>Map&lt;Integer,A&gt; myMap = new HashMap();
  
  
  for (Entry&lt;Integer,A&gt; entry : myMap)
  {
      int param1=entry.getValue().getParam1();
      List param2=entry.getValue().getParam2();
      for (B b : param2)
      {
          System.out.println(""+entry.getKey()+"-"+param1+"-"+b.toString());
      }
  }
  </code></pre>
  
  <p>Knowing that this example is a simplification of the problem (for example, i need "param1" more than once in the next for loop)</p>
  
  <p>So far, the only idea i have is to store all the informations i need into a tuple to finally use the forEach stream method over this tuple.
  (Not sure to be very clear....)</p>
  
  <p>Edit:I simplified my example too much. My case is more something like that:</p>
  
  <pre><code>Map&lt;Integer,A&gt; myMap = new HashMap();
  
  
  for (Entry&lt;Integer,A&gt; entry : myMap)
  {
      int param1=entry.getValue().getParam1();
      CustomList param2=entry.getValue().getParam2();
      for (int i = 0; i&lt;param2.size(); i++)
      {
          System.out.println(""+entry.getKey()+"-"+param1+"-"+param2.get(i).toString());
      }
  }
  </code></pre>
  
  <p>I could write something like that with stream:</p>
  
  <pre><code>myMap.entrySet().stream()
      .forEach(
          e -&gt; IntStream.range(0, e.getValue.getParam2().getSize())
               .forEach(
                  i -&gt; System.out.println(e.getKey()+"-"+e.getValue().getParam1()+"-"+e.getValue.getParam2.get(i))
               )
      );
  </code></pre>
  
  <p>However, what i have instead of "e.getValue.getParam2()" in my real case is much more complex (a sequence of 5-6 methods) and heavier than just retrieving a variable (it executes some logic), so i would like to avoid to repeat e.getValue.getParam2 (once in just before the forEach, and once in the forEach)</p>
  
  <p>i know that it's maybe not the best use case for using stream, but I am learning about it and would like to know about the limits</p>
  
  <p>Thanks!</p>