1
00:00:00,210 --> 00:00:02,490
The Stream pipeline replaces Lupe's.

2
00:00:05,240 --> 00:00:08,330
In this lesson, you're going to run an array list through a stream pipeline.

3
00:00:11,140 --> 00:00:15,940
First, open up the folder for this lesson by following this path in your course resources.

4
00:00:21,700 --> 00:00:27,100
Loops can get messy using loops, simple operations on an array list can get pretty messy.

5
00:00:31,220 --> 00:00:36,650
In this example, the first loop filters prices lower than five dollars and the second loop updates

6
00:00:36,650 --> 00:00:38,480
each price by adding the tax.

7
00:00:38,960 --> 00:00:40,730
This code is really tedious.

8
00:00:41,120 --> 00:00:42,770
Java offers a better way.

9
00:00:45,610 --> 00:00:52,150
The stream pipeline, a stream pipeline, is just a series of functions, in other words, you can run

10
00:00:52,150 --> 00:00:54,640
the rails through a series of functions.

11
00:00:56,730 --> 00:01:02,790
First, you need to treat the aerialist as a stream, a stream is a sequence of elements that can run

12
00:01:02,790 --> 00:01:03,770
through the pipeline.

13
00:01:07,560 --> 00:01:13,680
The first function in this pipeline is filter the filter function filters prices lower than five dollars.

14
00:01:17,080 --> 00:01:23,080
The updated sequence continues through the pipeline where the map function updates each price by adding

15
00:01:23,080 --> 00:01:27,550
the tax, if you ask me, this code is way cleaner and more concise.

16
00:01:30,890 --> 00:01:34,070
So filter filter is elements based on a boolean.

17
00:01:37,370 --> 00:01:44,360
It uses a lambda expression, and this is the syntax, the lambda expression receives each element and

18
00:01:44,360 --> 00:01:45,080
the stream.

19
00:01:46,860 --> 00:01:49,440
And it returns a boolean for each element.

20
00:01:51,530 --> 00:01:55,460
Now, for each is a terminal operation because it ends the pipeline.

21
00:01:56,770 --> 00:01:59,830
For each runs through the final stream of elements.

22
00:02:02,320 --> 00:02:04,600
Once again uses the lambda expression.

23
00:02:06,070 --> 00:02:11,380
The syntax is similar, where the lambda expression four for each receives each element in the stream.

24
00:02:12,440 --> 00:02:13,730
And executes covid.

25
00:02:17,760 --> 00:02:22,830
Filtered out, Java starts with an array list of four prices, a loop runs through the area list and

26
00:02:22,830 --> 00:02:24,330
filters the low prices.

27
00:02:30,260 --> 00:02:31,670
This code is pretty tedious.

28
00:02:31,940 --> 00:02:35,060
Instead, we're going to treat the aerialist as a stream.

29
00:02:40,110 --> 00:02:44,910
A stream is a sequence of elements that can run through a pipeline and the pipeline starts with the

30
00:02:44,910 --> 00:02:50,850
first function filter filter is going to update the stream of prices using a lambda expression that

31
00:02:50,850 --> 00:02:53,040
receives each price in the stream.

32
00:02:54,730 --> 00:02:56,320
And it needs to return a boolean.

33
00:02:58,850 --> 00:03:04,760
Lambda expressions use an arrow key to point to the expression, and here we can filter prices that

34
00:03:04,760 --> 00:03:06,410
are less than five dollars.

35
00:03:10,180 --> 00:03:16,060
This function goes through each element and filters the ones less than five dollars and discards the

36
00:03:16,060 --> 00:03:16,570
rest.

37
00:03:17,470 --> 00:03:22,780
The updated sequence of prices continues through the pipeline, but this pipeline is going to end with

38
00:03:22,780 --> 00:03:25,060
a terminal operation for each.

39
00:03:25,660 --> 00:03:31,600
For each uses a lambda expression and the lambda expression is going to receive each price from the

40
00:03:31,600 --> 00:03:33,520
updated sequence of elements.

41
00:03:33,700 --> 00:03:39,400
And once again, an arrow key points to the expression, and here we can write anything we want for

42
00:03:39,400 --> 00:03:41,500
each doesn't expect us to return anything.

43
00:03:41,650 --> 00:03:43,450
So I'm just going to print each element.

44
00:03:56,020 --> 00:04:02,530
And Perfect's, if you ask me, running neorealist through a pipeline is a lot cleaner than using Lupe's.

45
00:04:10,840 --> 00:04:13,160
But in fact, we can clean it up even more.

46
00:04:13,720 --> 00:04:20,050
The arrow key points to a block of code, but if you only have one line, the arrow, he can point directly

47
00:04:20,050 --> 00:04:26,620
to the value that your returning filter expects a boolean so it knows you intend on returning this boolean.

48
00:04:29,200 --> 00:04:34,390
But even if the function is void for each is void, if it's only one line, you can remove the curly

49
00:04:34,390 --> 00:04:35,110
brackets.

50
00:04:36,970 --> 00:04:38,200
This looks perfect.

51
00:04:38,380 --> 00:04:41,620
Notice how elegant this looks compared to using a loop.

52
00:04:45,830 --> 00:04:47,810
I'm going to now move on to the next file.

53
00:04:51,210 --> 00:04:55,750
Collect is a terminal operation because it signifies the end of a pipeline.

54
00:04:56,250 --> 00:04:58,830
It returns the updated sequence of objects.

55
00:05:04,200 --> 00:05:11,190
This code collects the updated stream of objects and returns it as a list array list is an implementation

56
00:05:11,190 --> 00:05:13,860
of list so you can convert between less than array list.

57
00:05:14,040 --> 00:05:15,160
Don't worry too much about that.

58
00:05:15,180 --> 00:05:18,440
For now, we're going to talk more about the relationship and inheritance.

59
00:05:18,450 --> 00:05:24,960
For now, just know that you can convert between list an array list anyways, inside filter to Java.

60
00:05:28,030 --> 00:05:30,550
Low prices has the following reference.

61
00:05:32,580 --> 00:05:38,310
The function receives the reference as the parameter, both the parameter and outside variable share

62
00:05:38,310 --> 00:05:40,040
a reference to the same array list.

63
00:05:40,350 --> 00:05:44,370
And so here we're updating elements in the array list with low prices.

64
00:06:02,800 --> 00:06:07,360
Once again, loops are messy and it's easier to use the original list as a stream.

65
00:06:15,670 --> 00:06:20,920
We can run the stream of price objects through our pipeline first, we filter prices using the same

66
00:06:20,920 --> 00:06:22,060
syntaxes before.

67
00:06:23,150 --> 00:06:28,910
Filter expects us to return a predicate, a boolean results, and we can use the arrow key to point

68
00:06:28,910 --> 00:06:35,000
directly to the boolean that we're returning and the updated sequence of prices continues through the

69
00:06:35,000 --> 00:06:35,630
pipeline.

70
00:06:35,630 --> 00:06:37,780
But there is nothing left we want to update.

71
00:06:38,060 --> 00:06:43,760
So we're going to end the pipeline with the terminal operation, collect, collect, accepts a collector

72
00:06:43,910 --> 00:06:47,750
and from the list of collectors, we're going to use the collector to list.

73
00:06:49,980 --> 00:06:54,480
To list is going to collect the updated stream of elements and return it as a list.

74
00:07:00,470 --> 00:07:07,220
You can't store a list of objects in a variable of type array list, but the array list can add all

75
00:07:07,220 --> 00:07:08,990
of the elements from the results.

76
00:07:16,880 --> 00:07:17,810
Let's run this code.

77
00:07:33,970 --> 00:07:39,610
And perfect to the collector is a terminal operation, so it ends the pipeline by returning the list

78
00:07:39,610 --> 00:07:40,480
of elements.

79
00:07:44,490 --> 00:07:49,830
Let's recap in this lesson, you learn to run a railroad through a stream pipeline, the stream pipeline

80
00:07:49,830 --> 00:07:56,820
replaces Loop's Filter is an example of an intermediate operation because it updates the stream by filtering

81
00:07:56,820 --> 00:08:00,360
elements based on a predicate and it continues the pipeline.

82
00:08:02,450 --> 00:08:08,390
A terminal operation signifies the end of a pipeline for each is a terminal operation because it ends

83
00:08:08,390 --> 00:08:11,360
the pipeline, but it's void so it doesn't return anything.

84
00:08:11,720 --> 00:08:15,890
It just runs through each element in the updated stream and executes random code.

85
00:08:18,570 --> 00:08:24,240
Is also a terminal operation because it ends the pipeline, but it returns the updated sequence of elements.

