title: Java8 : how to aggregate objects from a stream?
body: <h2>Edit</h2>
  
  <p><i>IMHO : I think it is not a duplicate because the two questions are trying to solve the problem in different ways and especially because they provide totally different technological skills  (and finally, because I ask myself these two questions).</i></p>
  
  <h2>Question</h2>
  
  <p>How to aggregate items from an ordered stream, preferably in an intermediate operation ?</p>
  
  <h2>Context</h2>
  
  <p>Following my other question : <a href="https://stackoverflow.com/questions/33868014/java8-stream-lines-and-aggregate-with-action-on-terminal-line">Java8 stream lines and aggregate with action on terminal line</a></p>
  
  <p>I've got a very large file of the form :</p>
  
  <pre><code>MASTER_REF1
      SUBREF1
      SUBREF2
      SUBREF3
  MASTER_REF2
  MASTER_REF3
      SUBREF1
      ...
  </code></pre>
  
  <p>Where SUBREF (if any) is applicable to MASTER_REF and both are complex objects (you can imagine it somewhat <em>like</em> JSON).</p>
  
  <p>On first look I tried to group the lines with an operation returning <code>null</code> while agregating and a value when a group of line could be found (a "group" of lines ends if <code>line.charAt(0)!=' '</code>).</p>
  
  <p>This code is hard to read and requires a <code>.filter(Objects::nonNull)</code>.</p>
  
  <p>I think one could achieve this using a <code>.collect(groupingBy(...))</code> or a <code>.reduce(...)</code> but those are terminal operations which is :</p>
  
  <ul>
  <li>not required in my case : lines are ordered and should be grouped by their position and groups of line are to be transformed afterwards (map+filter+...+foreach);</li>
  <li>nor a good idea : I'm talking of a huge data file that is way bigger than the total amount of RAM+SWAP ... a terminal operation would saturate availiable resources (as said, by design I need to keep groups in memory because are to be transformed afterwards)</li>
  </ul>