Is there any way to stop a Stream.generate from its Lambda closure?

Java 9 and later includes this method:

Stream<T> takeWhile(Predicate<? super T> predicate); 

to limit a stream by condition. So the workaround beneath is not needed anymore.

Original answer (for Java versions earlier than 9):

With Stream.generate this is per definition not possible from a lambda closure. It is by definition endless. Using limit() you are able make your stream fix sized. But this will not help you for conditions like:

if random>10 then stop

There is a possibility to limit a potential endless stream by condition. This is usefull if one does not know the size. Your friend here is a Spliterator and your sample code would look like:

System.out.println( StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<Integer>() {
    int counter = 0;

    @Override
    public boolean hasNext() {
        return counter < 10;
    }

    @Override
    public Integer next() {
        counter++;
        return RandomUtils.nextInt(100);
    }
}, Spliterator.IMMUTABLE), false).count());

Basically you are able to build a Stream from an Iterator. I am using this construct e.g. for a stream of XMLEvents from Stax XML - parsing.

I know this is not done by lambda constructs but it IHMO solves this lacking feature of stopping the stream item generation by condition.

I would be very interested, if there is a better way to achieve this (I mean this stream construct and not the XML processing ;)) or if there is a fundamental flaw in using streams in this way.


This is not possible with Lamdas, you cannot control the flow from inside the expression. Even the API docs says that the Stream.generate generates an infinite stream.

However, you can limit the Stream and achieve the desired functionality simply by using the limit() method:

System.out.println(Stream.generate(() -> RandomUtils.nextInt(100)).limit(10).count());

// If you are not looking for parallelism, you can use following method:
public static <T> Stream<T> breakStream(Stream<T> stream, Predicate<T> terminate) { 
  final Iterator<T> original = stream.iterator();
  Iterable<T> iter = () -> new Iterator<T>() { 
    T t;
    boolean hasValue = false;

    @Override
    public boolean hasNext() { 
      if (!original.hasNext()) { 
        return false;
      } 
      t = original.next();
      hasValue = true;
      if (terminate.test(t)) { 
        return false;
      } 
      return true;
    } 

    @Override
    public T next() { 
      if (hasValue) { 
        hasValue = false;
        return t;
      } 
      return t;
    } 
  };

  return StreamSupport.stream(iter.spliterator(), false);
}