title: Java Stream: is there a way to iterate taking two elements a time instead of one?
body: <p>Let's say we have this stream</p>
  
  <pre><code>Stream.of("a", "b", "err1", "c", "d", "err2", "e", "f", "g", "h", "err3", "i", "j");
  </code></pre>
  
  <p>and I want to save in a map the couples of adjacent strings in which the first one starts with "err". </p>
  
  <p>What I thought of is something like this</p>
  
  <pre><code>Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  
  Stream.of("a", "b", "err1", "c", "d", "err2", "e", "f", "g", "h", "err3", "i", "j")
  .reduce((acc, next) -&gt; {
      if (acc.startsWith("err"))
          map.put(acc,next);
      if (next.startsWith("err"))
          return next;
      else
          return "";
  });
  </code></pre>
  
  <p>But I'm not totally satisfied with it for two main reasons</p>
  
  <ol>
  <li>I'm "misusing" <code>reduce</code> function. In Stream API every function has its clear, well defined purpose: <code>max</code> is supposed to calcuate max value, <code>filter</code> is supposed to filter based on a condition, <code>reduce</code> is supposed to produce an incrementally accumulated value and so on.</li>
  <li>Doing that prevents me from using Streams powerful mechanisms: what if I wanted to limit my search to the first two results? </li>
  </ol>
  
  <p>Here I used <code>reduce</code> because (as far as I know) it's the only function that lets you compare couple of values that you can, somehow, lead back to something similar to "current value" and "next value" concepts.</p>
  
  <p>Is there a more straightforward way? Something that allows you to iterate the stream considering more than one value for each iteration?</p>
  
  <h1>EDIT</h1>
  
  <p>What I'm thinking about is some mechanism that, given the current element, allows you to define a "window of elements" to consider, for each iteration.</p>
  
  <p>Something like</p>
  
  <pre><code>&lt;R&gt; Stream&lt;R&gt; mapMoreThanOne(
      int elementsBeforeCurrent,
      int elementsAfterCurrent,
      Function&lt;List&lt;? super T&gt;, ? extends R&gt; mapper);
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>&lt;R&gt; Stream&lt;R&gt; map(Function&lt;? super T, ? extends R&gt; mapper);
  </code></pre>
  
  <p>That would be a powerful "upgrade" to current API. </p>
  
  <h1>EDIT2</h1>
  
  <p>I appreciate the effort of people proposing their solution, but the issue is not the algorithm per se. There are different ways to achieve my goal by putting together streams, indexes, temp variables to store previous values... but I was wondering if there was some method in Stream API that was designed for the task of dealing with elements other than the current without breaking the "stream paradigm". Something like this</p>
  
  <pre><code>List&lt;String&gt; list =
          Stream.of("a", "b", "err1", "c", "d", "err2", "e", "f", "g", "h", "err3", "i", "j")
          .filterFunctionImWonderingIfExist(/*filters couples of elements*/)
          .limit(2)
          .collect(Collectors.toList());
  </code></pre>
  
  <p>Given the answers, I think there's no "clear and quick" solution, unless using StreamEx library</p>