Hi All,

Today we will look at stream intermediate operations using Java 8.

So let's get started.

What are intermediate operations? Intermediate operations are stream operations that return another stream, allowing us to chain several stream operations together.

What are terminal operations? Terminal operations are stream operations that return void or non-stream results, hence it's called terminal operations.

Intermediate Operations 

  • Intermediate operations are always lazy, which means they are not evaluated until the terminal operation is called.
  • Intermediate operations are further classified as stateless and stateful.
  • Stateless operations do not keep the state of a previously seen element, implying that each element of a stream is processed independently. Filter and map as examples
  • Stateful operations - keep a previously seen element's state. Before producing output, these operations must process the entire stream. Distinct and sorted examples
Below are some of the intermediate operations.
  • Stream.filter()
  • Stream.map()
  • Stream..sorted()
  • Stream.skip()
  • Stream.limit()
  • Stream.distinct()
  • Stream.flatMap()
Stream.filter() - is an intermediate operator that returns all elements that match a given condition as specified by the predicate argument.

Because a predicate is a functional interface, we can pass a lambda expression as well.

The filter method's syntax is shown below.

Stream<T> filter(Predicate<? super T> predicate);

So let's look at an example to better understand filter.


So here we have taken an example of the filter by implementing Predicate and a lambda expression.
O/P

 
Stream.map() -It's intermediate and lazy in nature, and it's used to apply any function to each element of the stream.

Let's look at an example to better understand the map. Here, we will apply a 10% discount to each book and convert the title to upper case.
O/P
Stream.sorted() -is an intermediate operation used to sort streams in ascending and descending order; we must pass an instance of comparator as shown below for a list of custom objects.

We must use a reversed method to sort in descending order.
Example

O/P

Stream.skip()-is an intermediate operation that skips the first n elements of a stream; the parameter n must be positive, and if n exceeds the size of the stream, it returns an empty stream.

O/P
Stream.limit(n) - is an intermediate operation that returns a stream of size n, where n cannot be negative.

Example
O/P

Stream.distinct() - returns a stream containing a single distinct element from the given input stream It's an intermediate stateful operation. To find distinct elements, it employs the equals method.

O/P
1
3
4
2
7

Stream.flatMap()- It is an intermediate operation used for flattening + mapping. We've already seen mapping used above.

Flattening - is used to make the List flat.

Example Converting  List<List<String >> to List<String>
Or List<List<List<String >>> to List<List<String >>

Example 
Here we add 2 lists inside List<List<String>> and we will then flat them to a single List<String>

O/P

[Rajesh, Pratik, Suresh, Akash]

So far, we've covered the majority of the intermediate operators in Java 8.

Please let me know if you have any further questions.

Thanks
Enjoy your learning!

You can refer to this helpful post.




For spring visit Spring Tutorial