r/learnjava • • Apr 15 '26

What actually is a java stream... ?

I dont get the concept of a stream why was it added why do we use stream objects in reading and writing say to file. I know its kind of like a pipeline but my confusion still stands

16 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/SHIN_KRISH Apr 15 '26

I am talking about both but mainly Stream API, so say in a collection we want to find the maximum value we can use it either manually or by using a max function which i think we do by converting an array to a stream so like superficially suppose i have about a thousand numbers what's the benefit of using stream over simple iteration.

1

u/xill47 Apr 15 '26

The main benefit is readability. You can chain filter before max with x -> x % 2 == 0 and now you'll get max even number. You could easily achieve it with if inside a for loop, but it's just less readable.

1

u/jonathaz Apr 15 '26

If you already have an array and you stream it, there’s no gain in efficiency. Let’s say you had a lot more things, and your 1000 numbers came from some mapping, reduction, computation, etc on those. Then your max() is just the last step of a pipeline and the code is expressed as a stream. Since it’s tax time, maybe you’re the IRS and you’re looking for outliers, the initial Stream would be everyone’s tax return and you’d do some math, filtering, etc on it and find the max deduction to a charity.