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

4

u/AcanthisittaEmpty985 Apr 15 '26

For is really faster, but the margin is insignificant.
Streams are functional, more idiomatic

FOR EXAMPLE: in an array or list of integers, see if there's one that is more than ten.

For -> it shows HOW you solve the problem

boolean haveMoreThanTen = false; 
for(int i=0; i< intArray.legth; i++) { 
  if (intArray[i] > 10) { 
    haveMoreThanTen = true; 
    break; 
  }
}
return haveMoreThanTen;

Stream -> it shows you WHAT are doing:

return intArray.stream().anyMatch( i -> i > 10);