title: Rx - Divide stream into segments (lists) by condition
body: <p>I have an RX producer that creates a stream of strings like so (simplified version of the real stream):</p>
  
  <p><code>A1 A2 A3 B1 B2 C1 C2 C3 C4 C5 C6....</code></p>
  
  <p>The stream is endless, but ordered. So after the strings that start with <code>A</code> run out, <code>B</code> starts. When<code>B</code> runs out, <code>C</code> starts... when<code>Z</code> run out, we move to <code>AA1</code> etc. There's an unknown number of <code>A</code>'s, <code>B</code>'s etc, but it's typically 10-30 instances per letter.</p>
  
  <p>I'm looking for a way to divide this stream into blocks of all A's: <code>A1 A2 A3</code>, all B's: <code>B1 B2</code>, all C's: <code>C1 C2 C3 C4 C5 C6</code> etc. Each block can be either an observable (which I'll turn into a list) or simply a list.</p>
  
  <p>I tried several different approaches using RxJava, all of which failed. Among the things that didn't work are:</p>
  
  <ul>
  <li><p><strong>Group by</strong>: since the stream is endless, the per-letter observable doesn't complete. So when the A's run out and the B's start, A's Observable doesn't complete. So there's an ever increasing number of observables.</p></li>
  <li><p><strong>Window/Buffer with distinctUntilChanged</strong> - I use "distinctUntilChanged" on the original stream to output the first item of each group (the first A, first B etc). Then I use that stream as an input to <code>window</code> or a "buffer" operator to be used as a boundary between windows/buffers. That didn't work and all I got was empty lists.</p></li>
  </ul>
  
  <p>What's a correct solution using RX?
  I'd prefer a Java solution, but solutions in other implementations of RX that can be easily converted to Java are also very welcome.</p>