Marko Topolnik Marko Topolnik - 1 year ago 46
Java Question

Partition a Stream by a discriminator function

Note: this is a self-answered question.

One of the missing features in the Streams API is the "partition by" transformation, for example as defined in Clojure. Say I want to reproduce Hibernate's fetch join: I want to issue a single SQL SELECT statement to receive this kind of objects from the result:

class Family {
String surname;
List<String> members;

I issue

FROM Family f JOIN Member m on m.family_id =

and I retrieve a flat stream of
records. Now I need to transform it into a stream of
objects, with a list of its members inside. Assume I already have a
; now I need to transform it into a
and then act upon that with a mapping transformation which turns it into a

The semantics of the transformation are as follows: keep collecting the stream into a
for as long as the provided discriminator function keeps returning the same value; as soon as the value changes, emit the
as an element of the output stream and start collecting a new

I hope to be able to write this kind of code (I already have the

Stream<ResultRow> dbStream = resultStream(queryBuilder.createQuery(
+ " FROM Family f JOIN Member m on m.family_id ="
+ " ORDER BY"));
Stream<List<ResultRow> partitioned = partitionBy(r -> r.string(0), dbStream);
Stream<Family> = -> {
Family f = new Family(rs.get(0).string(0));
f.members = -> r.string(1)).collect(toList());
return f;

Needless to say, I expect the resulting stream to stay lazy (non-materialized) as I want to be able to process a result set of any size without hitting any O(n) memory limits. Without this crucial requirement I would be happy with the provided

Answer Source

The solution requires us to define a custom Spliterator which can be used to construct the partitioned stream. We shall need to access the input stream through its own spliterator and wrap it into ours. The output stream is then constructed from our custom spliterator.

The following Spliterator will turn any Stream<E> into a Stream<List<E>> provided a Function<E, ?> as the discriminator function. Note that the input stream must be ordered for this operation to make sense.

public class PartitionBySpliterator<E> extends AbstractSpliterator<List<E>> {
  private final Spliterator<E> spliterator;
  private final Function<? super E, ?> partitionBy;
  private HoldingConsumer<E> holder;
  private Comparator<List<E>> comparator;

  public PartitionBySpliterator(Spliterator<E> toWrap, Function<? super E, ?> partitionBy) {
    super(Long.MAX_VALUE, toWrap.characteristics() & ~SIZED | NONNULL);
    this.spliterator = toWrap;
    this.partitionBy = partitionBy;

  public static <E> Stream<List<E>> partitionBy(Function<E, ?> partitionBy, Stream<E> in) {
    return PartitionBySpliterator<>(in.spliterator(), partitionBy), false);

  @Override public boolean tryAdvance(Consumer<? super List<E>> action) {
    final HoldingConsumer<E> h;
    if (holder == null) {
      h = new HoldingConsumer<>();
      if (!spliterator.tryAdvance(h)) return false;
      holder = h;
    else h = holder;
    final ArrayList<E> partition = new ArrayList<>();
    final Object partitionKey = partitionBy.apply(h.value);
    boolean didAdvance;
    do partition.add(h.value);
    while ((didAdvance = spliterator.tryAdvance(h))
        && Objects.equals(partitionBy.apply(h.value), partitionKey));
    if (!didAdvance) holder = null;
    return true;

  static final class HoldingConsumer<T> implements Consumer<T> {
    T value;
    @Override public void accept(T value) { this.value = value; }

  @Override public Comparator<? super List<E>> getComparator() {
    final Comparator<List<E>> c = this.comparator;
    return c != null? c : (this.comparator = comparator());

  private Comparator<List<E>> comparator() {
    final Comparator<? super E> innerComparator =
                .orElse((Comparator) naturalOrder());
    return (left, right) -> {
      final int c =, right.get(0));
      return c != 0? c :
          left.get(left.size() - 1), right.get(right.size() - 1));