title: How to transform a Java stream into a sliding window?
body: <p>What is the recommended way to transform a stream into a sliding window?</p>
  
  <p>For instance, in Ruby you could use <a href="http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-each_cons" rel="noreferrer">each_cons</a>:</p>
  
  <pre><code>irb(main):020:0&gt; [1,2,3,4].each_cons(2) { |x| puts x.inspect }
  [1, 2]
  [2, 3]
  [3, 4]
  =&gt; nil
  irb(main):021:0&gt; [1,2,3,4].each_cons(3) { |x| puts x.inspect }
  [1, 2, 3]
  [2, 3, 4]
  =&gt; nil
  </code></pre>
  
  <p>In Guava, I found only <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Iterators.html#partition(java.util.Iterator,%20int)" rel="noreferrer">Iterators#partition</a>, which is related but no sliding window:</p>
  
  <pre><code>final Iterator&lt;List&lt;Integer&gt;&gt; partition =
     Iterators.partition(IntStream.range(1, 5).iterator(), 3);
  partition.forEachRemaining(System.out::println);
  --&gt;
  [1, 2, 3]
  [4]
  </code></pre>