title: Replace nested loop with Java 8 flatmap
body: <p>I'm trying to use flatmap to make a nested loop with the Stream API, but I can't seem to figure it out. As an example, I want to recreate the following loop:</p>
  
  <pre><code>List&lt;String&gt; xs = Arrays.asList(new String[]{ "one","two", "three"});
  List&lt;String&gt; ys = Arrays.asList(new String[]{"four", "five"});
  
  System.out.println("*** Nested Loop ***");
  for (String x : xs)
      for (String y : ys)
          System.out.println(x + " + " + y);
  </code></pre>
  
  <p>I can do it like this, but this seems ugly:</p>
  
  <pre><code>System.out.println("*** Nested Stream ***");
  xs.stream().forEach(x -&gt;
      ys.stream().forEach(y -&gt; System.out.println(x + " + " + y))
  );
  </code></pre>
  
  <p>Flatmap looks promising, but how can I access the variable in the outer loop?</p>
  
  <pre><code>System.out.println("*** Flatmap *** ");
  xs.stream().flatMap(x -&gt; ys.stream()).forEach(y -&gt; System.out.println("? + " + y));
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>*** Nested Loop ***
  one + four
  one + five
  two + four
  two + five
  three + four
  three + five
  *** Nested Stream ***
  one + four
  one + five
  two + four
  two + five
  three + four
  three + five
  *** Flatmap *** 
  ? + four
  ? + five
  ? + four
  ? + five
  ? + four
  ? + five
  </code></pre>