Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 2
    Why are Streams hard to construct? According to this article, it seems easy: oracle.com/technetwork/articles/java/… Commented Mar 8, 2016 at 14:06
  • 2
    There are quite a number of classes that have 'stream' method: collections, input streams, directory files, etc. But what if you want to create a stream from a custom loop - say, iterating over database cursor? The best way I've found so far is to create an Iterator, wrap it with Spliterator, and finally invoke StreamSupport#fromSpliterator. Too much glue for a simple case IMHO. There is also Stream.iterate but it produces infinite stream. The only way to cut off sream in that case is Stream#anyMatch, but it's a terminal operation, thus you can't separate stream producer and consumer Commented Mar 8, 2016 at 17:47
  • 2
    RxJava has Observable.fromCallable, Observable.create and so on. Or you can safely produce infinite Observable, then say '.takeWhile(condition)', and you're ok with shipping this sequence to the consumers Commented Mar 8, 2016 at 17:49
  • 1
    Streams are not hard to construct by yourself. You can simply call Stream.generate() and pass your own Supplier<U> implementation, just one simple method from which you provide the next item in the stream. There are loads of other methods. To easily construct a sequence Stream that depends on previous values you can use the interate() method, every Collection has a stream() method and Stream.of() constructs a Stream from a varargs or array. Finally StreamSupport has support for more advanced stream creation using spliterators or for streams primitive types. Commented Nov 1, 2016 at 15:09
  • 1
    "Streams are lacking cut-off operations (takeWhile(), takeUntil());" - JDK9 has these, I believe, in takeWhile() and dropWhile() Commented Nov 30, 2017 at 16:09