title: Java 8 lambda: iterate over stream objects and use previous/next object(s) in stream
body: <p>I am practising some entry-level java 8 lambda functionality.</p>
  
  <p>Given a list of messages, each containing a message offset, where all offsets must form a consecutive list of integers, I'm trying to find gaps to warn about. I get the feeling this all should be well doable with a nice lambda. But I can't get my head around it.</p>
  
  <p>So, there's this working snippet:</p>
  
  <pre><code>private void warnAboutMessageGaps(final List&lt;Message&gt; messages) {
  
      final List&lt;Long&gt; offsets = messages.stream()
              .sorted(comparingLong(Message::getOffset))
              .map(Message::getOffset)
              .collect(toList())
              ;
  
      for (int i = 0; i &lt; offsets.size() - 1; i++) {
          final long currentOffset = offsets.get(i);
          final long expectedNextOffset = offsets.get(i) + 1;
          final long actualNextOffset = offsets.get(i + 1);
          if (currentOffset != expectedNextOffset) {
              LOG.error("Missing offset(s) found in messages: missing from {} to {}", currentOffset + 1, actualNextOffset - 1);
          }
      }
  }
  </code></pre>
  
  <p>What I can't figure out is how to make it so that I can do the "compare with previous/next object" in the lambda. Any pointers would be appreciated.</p>
  
  <p>/edit: Suggestions about StreamEx and other third-party solutions, while appreciated, are not what I was looking for.</p>