The Stream pipeline replaces Lupe's.
In this lesson, you're going to run an array list through a stream pipeline.
First, open up the folder for this lesson by following this path in your course resources.
Loops can get messy using loops, simple operations on an array list can get pretty messy.
In this example, the first loop filters prices lower than five dollars and the second loop updates
each price by adding the tax.
This code is really tedious.
Java offers a better way.
The stream pipeline, a stream pipeline, is just a series of functions, in other words, you can run
the rails through a series of functions.
First, you need to treat the aerialist as a stream, a stream is a sequence of elements that can run
through the pipeline.
The first function in this pipeline is filter the filter function filters prices lower than five dollars.
The updated sequence continues through the pipeline where the map function updates each price by adding
the tax, if you ask me, this code is way cleaner and more concise.
So filter filter is elements based on a boolean.
It uses a lambda expression, and this is the syntax, the lambda expression receives each element and
the stream.
And it returns a boolean for each element.
Now, for each is a terminal operation because it ends the pipeline.
For each runs through the final stream of elements.
Once again uses the lambda expression.
The syntax is similar, where the lambda expression four for each receives each element in the stream.
And executes covid.
Filtered out, Java starts with an array list of four prices, a loop runs through the area list and
filters the low prices.
This code is pretty tedious.
Instead, we're going to treat the aerialist as a stream.
A stream is a sequence of elements that can run through a pipeline and the pipeline starts with the
first function filter filter is going to update the stream of prices using a lambda expression that
receives each price in the stream.
And it needs to return a boolean.
Lambda expressions use an arrow key to point to the expression, and here we can filter prices that
are less than five dollars.
This function goes through each element and filters the ones less than five dollars and discards the
rest.
The updated sequence of prices continues through the pipeline, but this pipeline is going to end with
a terminal operation for each.
For each uses a lambda expression and the lambda expression is going to receive each price from the
updated sequence of elements.
And once again, an arrow key points to the expression, and here we can write anything we want for
each doesn't expect us to return anything.
So I'm just going to print each element.
And Perfect's, if you ask me, running neorealist through a pipeline is a lot cleaner than using Lupe's.
But in fact, we can clean it up even more.
The arrow key points to a block of code, but if you only have one line, the arrow, he can point directly
to the value that your returning filter expects a boolean so it knows you intend on returning this boolean.
But even if the function is void for each is void, if it's only one line, you can remove the curly
brackets.
This looks perfect.
Notice how elegant this looks compared to using a loop.
I'm going to now move on to the next file.
Collect is a terminal operation because it signifies the end of a pipeline.
It returns the updated sequence of objects.
This code collects the updated stream of objects and returns it as a list array list is an implementation
of list so you can convert between less than array list.
Don't worry too much about that.
For now, we're going to talk more about the relationship and inheritance.
For now, just know that you can convert between list an array list anyways, inside filter to Java.
Low prices has the following reference.
The function receives the reference as the parameter, both the parameter and outside variable share
a reference to the same array list.
And so here we're updating elements in the array list with low prices.
Once again, loops are messy and it's easier to use the original list as a stream.
We can run the stream of price objects through our pipeline first, we filter prices using the same
syntaxes before.
Filter expects us to return a predicate, a boolean results, and we can use the arrow key to point
directly to the boolean that we're returning and the updated sequence of prices continues through the
pipeline.
But there is nothing left we want to update.
So we're going to end the pipeline with the terminal operation, collect, collect, accepts a collector
and from the list of collectors, we're going to use the collector to list.
To list is going to collect the updated stream of elements and return it as a list.
You can't store a list of objects in a variable of type array list, but the array list can add all
of the elements from the results.
Let's run this code.
And perfect to the collector is a terminal operation, so it ends the pipeline by returning the list
of elements.
Let's recap in this lesson, you learn to run a railroad through a stream pipeline, the stream pipeline
replaces Loop's Filter is an example of an intermediate operation because it updates the stream by filtering
elements based on a predicate and it continues the pipeline.
A terminal operation signifies the end of a pipeline for each is a terminal operation because it ends
the pipeline, but it's void so it doesn't return anything.
It just runs through each element in the updated stream and executes random code.
Is also a terminal operation because it ends the pipeline, but it returns the updated sequence of elements.