Videos
I was reading Java 8 in action ( I know quite late to the party) and got intrigued into thinking as to why streams got introduced? I understand they have many benefits but I am curious to know what specific limitations of earlier Java versions propelled this?
I have read through multiple guides but I still don't get the concept of streams. What exactly are they and why are they important? Why dont we just use arrayLists instead?
So let me tell you what I am exactly looking for. The basic question is, "Why was Java Stream API introduced so recently? Why wasn't it introduced earlier?" I am listing down the key points of the discussion and my comments(if any) as per my knowledge:
Working on the idea, the algorithm and developing the API took time.
To compete with JavaScript's Array features.
Because Streams are faster than For Loops. (I don't think so. For Loop is faster than Streams.)
To make use of Multi-Core CPUs. (Counter Point: That could've been achieved through a multi-threaded environment.)
So I want to understand the API more precisely. I am curious about the internal working and yes, if someone could answer the base question, it would probably help me end the discussion. Any help is appreciated.
Edit: Peak points of discussion as in the points from what we discussed and not from the discussion over the internet.
I have an hard time understanding a good use case for Java Streams.
My experience is mainly related to Web applications. Most things happens at DB level, or controller level. Business logic not too complex and definitively not handling big arrays.
I had some experience with ETL, but rather on analysing quickly many small files.
I find old for loops much easier to understand and maintain, yes more verbose for sure, but that's it. One-liners with streams look cool, right...
Performance wise, I think I would need to process a load of data to really see a difference.
The only big reason I see to study them it's because they are subject of questions in job interviews...
But I'm sure I am wrong, please give me some light.
Check out these talks by Venkat Subramaniam. There's one where he talks about "simple vs. familiar" that you need to hear. TL;DR, people often mistakenly say something isn't simple when what they really mean is it's unfamiliar.
Maybe you can figure out how to search the transcripts to find it. But they're all worth watching.
https://youtu.be/1OpAgZvYXLQ
https://youtu.be/WN9kgdSVhDo
https://youtu.be/kG2SEcl1aMM
I like Streams and the map/reduce style (what it's called generically since the term stream is pretty Java specific), because it tells you what it's doing. filter does exactly that. The equivalent with a for loop is an if or continue which isn't as clear because you have to run the code in your head to figure out what it's doing. With a stream I can see that it's filtering and if I want to I can dig into exactly how, but I don't have to get an overall feel for what's going on. Likewise findFirst says exactly what it's doing. break means it's exiting early but doesn't mean it's done after the first thing.
I find old for loops much easier to understand and maintain
This is almost certainly a lack of familiarity with them. Once I started using them, I quickly found that I prefer them. When working with lists, which is very often in my experience, they are much easier to read.