title: Group by object property in java flux
body: <p>Given the following data structure <code>Data</code> and <code>Flux&lt;Data&gt;</code> what is idiomatic way to achieve grouping into series of lists based on some property:</p>
  
  <pre><code>import org.reactivestreams.Publisher;
  import reactor.core.publisher.Flux;
  import reactor.core.publisher.Mono;
  
  import java.util.List;
  import java.util.Objects;
  import java.util.concurrent.atomic.AtomicReference;
  import java.util.function.Function;
  import java.util.function.Predicate;
  
  class Scratch {
      private static class Data {
          private Integer key;
          private String value;
  
          public Data(Integer key, String value) {
              this.key = key;
              this.value = value;
          }
  
          public Integer getKey() {
              return key;
          }
  
          public String getValue() {
              return value;
          }
  
          public static Data of(Integer key, String value) {
              return new Data(key, value);
          }
  
          @Override
          public String toString() {
              return value;
          }
      }
  
      public static void main(String[] args) {
          Flux&lt;Data&gt; test = Flux.just(
                  Data.of(1, "Hello"),
                  Data.of(1, "world"),
                  Data.of(2, "How"),
                  Data.of(2, "are"),
                  Data.of(2, "you"),
                  Data.of(3, "Bye"));
          test.bufferUntil(new Predicate&lt;Data&gt;() {
              Integer prev = null;
              @Override
              public boolean test(Data next) {
                  boolean collect = prev != null &amp;&amp; !Objects.equals(prev, next.getKey());
                  prev = next.getKey();
                  return collect;
              }
          }, true).subscribe(e -&gt; System.out.println(e.toString()));
      }
  } 
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>[Hello, world]
  [How, are, you]
  [Bye]
  </code></pre>
  
  <p>I am aware of groupBy function on Flux, but this gives me again a Flux, not a list. Current solution I have described above works, but it does not feel 100% idiomatic because I had to use anonymous class instead of lambda. I could have use lambda and AtomicReference outside from lambda, but that too does not feel 100% right. Any suggestions?</p>