The stream is the most widely used feature of Java 8. It provides visualization as a pipeline with data. The stream is not a storage or data structure that stores elements but the internal iteration of its elements. Stream consumed only once.
The stream used for collections to filter-map-reduce operations that produce a result without modifying the main source. While Collection has a finite size stream is infinite.
Useful operations for Stream:
- filter()
- map()
- flatMap()
- distinct()
- peek()
- anyMatch()
- allMatch()
- noneMatch()
- collect()
- count()
- findAny()
- findFirst()
- forEach()
- min()
- max()
- reduce()
- toArray()
Limitations of Streams:
1. Once a Stream is consumed, it can’t be used later on. As you can see in the above examples that every time I created a stream.
2. Lambda expressions (as well as anonymous classes) in Java can only access to the final (or effectively final) variables of the enclosing scope.
3. There are a lot of methods in Stream API and so the most confusing part is the overloaded methods. It makes the learning curve time taking. So the first time it takes some time to learn about how it works.
4. Stateless lambda expressions: If you are using parallel stream and lambda expressions are stateful, it can result in random responses. Let’s see this with a simple program.
Leave your thought here