title: Java 8 Stream of sentences
body: <p>I’d like to use Java 8 streams to take a stream of strings (for example read from a plain text file) and produce a stream of sentences.  I assume sentences can cross line boundaries.</p>
  
  <p>So for example, I want to go from:</p>
  
  <pre><code>"This is the", "first sentence.  This is the", "second sentence."
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>"This is the first sentence.", "This is the second sentence."
  </code></pre>
  
  <p>I can see that it’s possible to get a stream of parts of sentences as follows:</p>
  
  <pre><code>Pattern p = Pattern.compile("\\.");
  Stream&lt;String&gt; lines
     = Stream.of("This is the", "first sentence.  This is the", "second sentence.");
  
  Stream&lt;String&gt; result = lines.flatMap(s -&gt; p.splitAsStream(s));
  </code></pre>
  
  <p>But then I’m not sure how to produce a stream to join the fragments into sentences.  I want to do this in a lazy way so that only what is needed from the original stream is read.  Any ideas?</p>