title: Tracing Streams
body: <p>I am working with Java 8 streams, and would like to come up with a way to debug them. So i thought I could write a filter that printed out the elements at a stage of the stream, something like this:</p>
  
  <pre><code>int[] nums = {3, -4, 8, 4, -2, 17, 9, -10, 14, 6, -12};
  int sum = Arrays.stream(nums)
      .filter(w -&gt; {System.out.print(" " + w); return true;}) // trace
      .map(n -&gt; Math.abs(n))
      .filter(w -&gt; {System.out.print(" " + w); return true;}) // trace
      .filter(n -&gt; n % 2 == 0)
      .distinct()
      .sum();
  System.out.println(sum);
  </code></pre>
  
  <p>Close, but that's not quite it, as it doesn't have the proper delimiters to make it legible:</p>
  
  <pre><code> 3 3 -4 4 8 8 4 4 -2 2 17 17 9 9 -10 10 14 14 6 6 -12 1256
  </code></pre>
  
  <p>What I want is:</p>
  
  <pre><code>[3, -4, 8, 4, -2, 17, 9, -10, 14, 6, -12]
  [3 4 8 4 2 17 9 10 14 6 12]
  56
  </code></pre>
  
  <p>Is there a more standard way to do this? Note that Peek as the linked article says does not do this, because I somehow want to collect all the elements of the stream at each stage together.</p>