title: Java 8 : Stream : Consuming steps to save Intermediate States of streamed elements
body: <p>First of all, sorry for that abstract title. The idea is more simple with an example. Let say I have some values in a list L. I want to build parameters Request for a service, then call this service and collect all the Responses.</p>
  
  <p>Currently, I'm using this kind of code structure :</p>
  
  <pre><code>private final List&lt;String&gt; bodies = ImmutableList.of(
          "1", "2", "3", "4"
  );
  
  @Test
  public void BasicRequestResponseStreamToList() {
  
      final List&lt;Request&gt; requests = bodies.stream()
              .map(Request::new)
              .collect(Collectors.toList());
  
      final List&lt;Response&gt; responses = requests.stream()
              .map(service::send)
              .collect(Collectors.toList());
  
      commonAssertions(requests, responses);
  
  }
  </code></pre>
  
  <p>However, I find the need of two stream not efficient considering the last request has to be built before the first one can be sent. I would like to do something like :</p>
  
  <pre><code>@Test
  public void StatefulMapperStreamRequestResponseToList() {
  
      final List&lt;Request&gt; requests = new ArrayList&lt;&gt;();
      final List&lt;Response&gt; responses = bodies.stream()
              .map(Request::new)
              .map(x -&gt; {
                  requests.add(x);
                  return service.send(x);
              })
              .collect(Collectors.toList());
  
      commonAssertions(requests, responses);
  
  }
  </code></pre>
  
  <p>However, I feel guilty to use such a "Hack" to the mapping semantic. However it's the only way I've found to build 2 Correlated list with lazy loading. The first solution doesn't interest me because it has to wait to build all the request before sending them.
  I would love to achieve something like a wiretap in EIP. <a href="http://camel.apache.org/wire-tap.html" rel="nofollow">http://camel.apache.org/wire-tap.html</a></p>
  
  <p>I would gladly have your thoughts about a more elegant manner than modifying the semantic of the map method to achieve this.</p>
  
  <p>If it helps, you can find the source here : <a href="http://tinyurl.com/hojkdzu" rel="nofollow">http://tinyurl.com/hojkdzu</a></p>