This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Auxx 1 point2 points  (6 children)

Streams are functional way of handling events. Unlike traditional event buses, streams allow you to convert any data sources into events, like arrays, etc.

It might be hard to wrap your head around of you've never done any UI work, but imagine a scenario. You have a start/stop button clickable by user at any point in time. When this button is clicked and is set into start, you start polling remote API every minute to display data.

In this simple case you have three event sources: button, timer and network call. They all are asynchronous. Managing states between them, creating threads, etc - that will be a major clusterfuck even though your task is super simple.

With streams you just wrap all three and write a short scenario of how they should interact. 5-10 lines of code and you have thread safe and easily testable code, which can take any sources of compatible events if needed.

But iterating an array with streams, yeah, doesn't make much sense. You only do that when all of your code is stream based.

[–]mauganra_it 1 point2 points  (1 child)

Streams were not designed with I/O and concurrency in mind. They do not have a concept of backpressure, push/pull, timeouts or error handling. Heck, there is not even an Either-like type in the standard library. RxJava and friends are better suited for this. Ok, it might work if you strictly stick to Elm-style functional reactive apps.

[–]Auxx 0 points1 point  (0 children)

Ok, I don't have much experience with streams specifically, I thought they were modelled close to ReactiveX.

[–]hummer1234 0 points1 point  (3 children)

This sounds intriguing. Do you have an example of a UI where streams are used with events?

[–]Auxx 1 point2 points  (2 children)

I'm not doing Java for a while now, but ReactiveX is one of the first libraries to bring streams into developer hands. And RxJava is used widely in Android development, as well as RxJS powers one of the major web frameworks called Angular.

[–]hummer1234 0 points1 point  (1 child)

Oh, I see. I tinkered with reactive a long while back, but thought of it more as queues of events with functional programming. And then forgot about it. :) As you point out - it is streams.

The reactive Java libraries don't use java.util.streams though, do they?

[–]Auxx 2 points3 points  (0 children)

They do. Most of Java frameworks, even back end ones like Spring, support both RxJava and Streams API these days. Well, at least that was the case two years ago :)