1
00:00:05,530 --> 00:00:10,670
I know there are students in this class and we're going to discuss topics that will help to make your

2
00:00:10,670 --> 00:00:13,900
code clean and concise while working with collections in the race.

3
00:00:14,300 --> 00:00:19,760
They will talk about Stream API at the beginning of the lesson, will give an answer what stream API

4
00:00:19,760 --> 00:00:20,180
is.

5
00:00:20,450 --> 00:00:26,400
I will explain to you why we need to use three API with race and collection sometimes after this class

6
00:00:26,420 --> 00:00:31,690
and you will understand what the conveyor belt is and what terminal and non terminal operations are.

7
00:00:32,240 --> 00:00:37,550
You are going to learn how to create stream and how to process elements with the help of Stream API.

8
00:00:37,790 --> 00:00:40,670
I'm going to show you really a lot of examples today.

9
00:00:40,880 --> 00:00:46,010
I will teach you how to create a stream from array and how to convert at to map with the help of Stream

10
00:00:46,010 --> 00:00:46,490
API.

11
00:00:46,820 --> 00:00:52,400
In this lesson, we are going to have a lot of practice because one of the ways to understand this topic

12
00:00:52,400 --> 00:00:56,330
better is to understand how you can apply this knowledge on practice.

13
00:00:56,630 --> 00:01:01,460
Let's start and to start with, let's create a definition of stream API.

14
00:01:02,030 --> 00:01:07,970
If we would open official documentation from Oracle for Jayaweera to Stream Package will see that this

15
00:01:07,970 --> 00:01:14,390
package contains classes to support functional style operations on streams of elements such as mass

16
00:01:14,390 --> 00:01:16,640
produced transformations, onco actions.

17
00:01:17,150 --> 00:01:22,180
I believe for people who didn't work with streams, this definition doesn't bring a lot of clarity.

18
00:01:22,580 --> 00:01:24,230
So let me explain you the same.

19
00:01:24,320 --> 00:01:31,160
In more simple words, stream application programming interface is a set of interfaces that allow to

20
00:01:31,160 --> 00:01:37,850
process elements and to perform operations with them in containers and arrays with the help of functional

21
00:01:37,850 --> 00:01:38,720
interfaces.

22
00:01:39,080 --> 00:01:45,560
So to perform operation on the group of elements, you can create lambda functions or use Macit references

23
00:01:45,560 --> 00:01:49,130
that in turn significantly simplifies your code.

24
00:01:49,550 --> 00:01:50,630
Does it make sense?

25
00:01:51,170 --> 00:01:56,690
Now let me perform an overview of one important interface from Jowett Ustream package.

26
00:01:56,840 --> 00:02:00,110
This will help you to understand our practice examples.

27
00:02:00,680 --> 00:02:05,030
I would say that one of the most important interfaces is stream interface.

28
00:02:05,450 --> 00:02:11,990
What it is for string interface represents a sequence of elements supporting sequential and parallel

29
00:02:11,990 --> 00:02:13,370
aggregate operations.

30
00:02:13,880 --> 00:02:15,380
This is hard to understand.

31
00:02:15,380 --> 00:02:17,120
Believe me, I know what you feel.

32
00:02:17,570 --> 00:02:24,650
Usually I ask my students to imagine conveyor belt and on this conveyor you have elements from your

33
00:02:24,650 --> 00:02:31,940
collection and as a conveyor belt rolling, you take each element from it and perform any modifications

34
00:02:31,940 --> 00:02:32,420
with it.

35
00:02:33,140 --> 00:02:38,900
Before we dive deeper to code examples, I want you to understand, at least on a high level, the next

36
00:02:38,900 --> 00:02:39,440
example.

37
00:02:39,830 --> 00:02:46,490
Imagine that you have a list of products and you have a task to keep on that products that has more

38
00:02:46,490 --> 00:02:53,000
than one thousand items in the warehouse and decrease price for these products on 10 percent.

39
00:02:53,540 --> 00:02:59,420
This business logic should change the price for products, and this in turn should motivate customers

40
00:02:59,420 --> 00:03:03,500
of the online store to buy these products while price is reduced.

41
00:03:04,190 --> 00:03:05,450
How would you do that?

42
00:03:05,960 --> 00:03:08,120
You would create the container of products.

43
00:03:08,120 --> 00:03:14,150
Let's imagine that this is a box that is full of products and put all products on conveyor belt.

44
00:03:14,750 --> 00:03:19,790
After that, you will check what products has more than one thousand items in warehouse.

45
00:03:20,210 --> 00:03:24,020
You will remove from Canberra products that you are not interested in.

46
00:03:24,470 --> 00:03:29,420
That means you have to apply a function that knows how to filter elements.

47
00:03:29,900 --> 00:03:32,990
After that, you will modify price for each product.

48
00:03:33,440 --> 00:03:40,880
That means you have to apply functions that modify state of the product and only after that the end

49
00:03:40,880 --> 00:03:44,990
of the conveyor belt, you would gather all elements together back to the box.

50
00:03:45,290 --> 00:03:52,070
Is it clear now you can understand that the functions that was applied for each element on the conveyor

51
00:03:52,070 --> 00:03:55,580
belt, they are called non terminal also.

52
00:03:55,580 --> 00:04:02,720
They might be called intermediate functions or it would be opportunism, conveyor operations and the

53
00:04:02,720 --> 00:04:06,710
mass that terminate conveyor are called terminal operations.

54
00:04:07,340 --> 00:04:15,020
We also can say that each non terminal operation at Listener to the Stream listener modifies the element

55
00:04:15,020 --> 00:04:17,120
and pass it to another listener.

56
00:04:17,540 --> 00:04:22,850
This is called stream processing terminal and non terminal operations.

57
00:04:22,850 --> 00:04:24,170
Create Stream Pipeline.

58
00:04:24,380 --> 00:04:31,880
Stream Pipeline consists of the source that is our collection of elements or array followed by intermediate

59
00:04:31,880 --> 00:04:34,640
operations and ended with terminal operation.

60
00:04:35,180 --> 00:04:37,100
Let's consider another case.

61
00:04:37,460 --> 00:04:39,590
You may have multiple lists of products.

62
00:04:39,920 --> 00:04:40,940
How it can be.

63
00:04:41,600 --> 00:04:48,020
You have different warehouses and each warehouse contains information about the products there.

64
00:04:48,530 --> 00:04:52,810
So you have multiple lists of products from each warehouse.

65
00:04:53,240 --> 00:04:59,300
This may happen when Demand-Driven design of your application declares that you have type warehouse

66
00:04:59,300 --> 00:05:01,850
that in turn can return list of products.

67
00:05:02,460 --> 00:05:08,910
And imagine that you have a list, our houses in our example, you have a lot of their houses with products

68
00:05:09,300 --> 00:05:15,840
we can create, can aware of their houses, but we can also have the functions that would tell how to

69
00:05:15,840 --> 00:05:20,120
extract products from these warehouses to have conveyor of products.

70
00:05:20,430 --> 00:05:25,920
And after that, we are going to have similar to previous example, logic will filter elements and will

71
00:05:25,920 --> 00:05:28,630
perform price modification, I believe.

72
00:05:28,650 --> 00:05:31,680
Now you understand what we are going to learn today.

73
00:05:32,070 --> 00:05:37,830
We're going to learn terminal and not terminal functions to perform operations with elements in streams.

74
00:05:38,730 --> 00:05:42,510
Now, let's look at the good examples that I prepared for this lesson.

75
00:05:42,510 --> 00:05:49,080
To understand how streams work will start from reproducing the case that you saw on slides.

76
00:05:49,590 --> 00:05:56,010
All classes are stored in one file to keep all examples related to this lesson grouped and to not switch

77
00:05:56,010 --> 00:05:58,190
between the different types during the lesson.

78
00:05:58,770 --> 00:06:03,920
Here we have a class in its price name and the amount of items in their house.

79
00:06:04,590 --> 00:06:10,400
Also, you can see a special constructor getters and setters to string Masset for the bag and purposes.

80
00:06:11,010 --> 00:06:15,510
And here you can see a warehouse type for the sake of our next examples.

81
00:06:15,540 --> 00:06:18,690
This class has only one field list of products.

82
00:06:19,110 --> 00:06:24,720
Also, there are two masses, getter and setter, to retrieve and to set products accordingly.

83
00:06:25,230 --> 00:06:27,630
Now imagine that I have a list of products.

84
00:06:27,960 --> 00:06:34,500
You can see that I created multiple different products here and put all of them into the list to create

85
00:06:34,510 --> 00:06:34,950
stream.

86
00:06:34,950 --> 00:06:40,920
From this list, I have to just go through Method Stream is the default method in collection interface.

87
00:06:41,190 --> 00:06:44,010
So all collections have be method.

88
00:06:44,460 --> 00:06:49,170
After we create a stream we can call intermediate operations and at least inners.

89
00:06:49,470 --> 00:06:54,840
For example, let's reproduce a situation from the slides that we saw at the beginning of our lesson

90
00:06:55,320 --> 00:06:56,710
I call filter method.

91
00:06:57,060 --> 00:06:58,920
Let me open the source code of Feltham.

92
00:06:58,920 --> 00:07:06,120
As you can see, that filter method takes predicate all intermediate functions, return the stream objects

93
00:07:06,510 --> 00:07:10,170
that allows us to create a chain of MassArt in locations.

94
00:07:10,680 --> 00:07:16,880
Filter message returns a stream consisting of the elements of the stream that match the given predicate.

95
00:07:17,400 --> 00:07:23,790
That means in case predicates returns true for some element, we should keep this element in the stream.

96
00:07:24,360 --> 00:07:26,040
Let's get back to the demo file.

97
00:07:26,790 --> 00:07:28,620
We use simple predicate here.

98
00:07:29,100 --> 00:07:35,760
We want to keep on our conveyor belt only products that have more than 1000 items in the warehouse.

99
00:07:36,270 --> 00:07:39,870
After that, I want to apply function to each element.

100
00:07:40,380 --> 00:07:42,390
I use math method for this.

101
00:07:42,930 --> 00:07:45,330
Let me open the source code of math, Macit.

102
00:07:46,080 --> 00:07:52,380
This message returns a stream consisting of the results of a the given function to the aliments in the

103
00:07:52,380 --> 00:07:52,890
stream.

104
00:07:53,280 --> 00:07:59,840
In our particular case, we modify product and we return the same product to the stream we set.

105
00:07:59,850 --> 00:08:02,940
The new price set is ten percent less than the original.

106
00:08:02,940 --> 00:08:07,730
One important thing to know here is that intermediate operations are lazy.

107
00:08:08,100 --> 00:08:14,460
This means that they will be invoked only if it is necessary for the terminal operation execution.

108
00:08:14,940 --> 00:08:21,210
Let's give you the understanding that this lambda functions will be executed only in case a terminal

109
00:08:21,210 --> 00:08:23,340
operation and the end of the chain.

110
00:08:23,820 --> 00:08:29,250
And now when I did everything I wanted, I want to create a collection of items that are currently on

111
00:08:29,250 --> 00:08:32,580
the conveyor belt I call terminal function collect.

112
00:08:33,000 --> 00:08:36,240
Let's review the source code of collect Macit here.

113
00:08:36,240 --> 00:08:39,660
We can see that this massive tax collector as an argument.

114
00:08:40,020 --> 00:08:41,580
But what is a collector?

115
00:08:42,030 --> 00:08:45,810
We have only one way to learn this from the source code.

116
00:08:45,810 --> 00:08:52,920
We can understand that collector is a specific type that accumulates input elements into mutable result

117
00:08:52,920 --> 00:08:53,660
content.

118
00:08:54,210 --> 00:08:56,880
But where to get the implementation of collector?

119
00:08:57,360 --> 00:09:01,730
Usually engineers use collectors glass to get any collector that is needed.

120
00:09:02,160 --> 00:09:08,280
Let me show you the source code of collectors glass this glass also from Jemmett Ustream package.

121
00:09:08,580 --> 00:09:13,050
And in the Masset outline you can see that we can get different collectors.

122
00:09:13,480 --> 00:09:20,400
The most popular, in my opinion, are the ones that are returned by the next masses to least to set

123
00:09:20,700 --> 00:09:21,330
the map.

124
00:09:21,870 --> 00:09:26,520
Also, you can get collectors that will collect all your elements to modify modifiable least.

125
00:09:26,520 --> 00:09:30,180
For example, most of the masses are describing here.

126
00:09:30,450 --> 00:09:34,610
That's why I don't see the sense into naming all of this matter.

127
00:09:35,160 --> 00:09:40,500
Just open the source code of this class to investigate what other collectors you can use.

128
00:09:40,920 --> 00:09:47,100
By the way, at the end of the lesson, I will leave a cheat sheet for you where I will gather all the

129
00:09:47,100 --> 00:09:50,490
most popular masses, including the terminal functions.

130
00:09:51,270 --> 00:09:53,190
Let's get back to our demo file.

131
00:09:53,670 --> 00:09:58,440
In this case, I want to group all elements from my stream into the container.

132
00:09:58,800 --> 00:10:01,650
I call collect method and pass collect the.

133
00:10:01,710 --> 00:10:10,080
Is it a might the least Masset implication, is this example clear so you can see massive change here

134
00:10:10,080 --> 00:10:14,990
with multiple intermediate functions and then I have one terminal operation.

135
00:10:15,510 --> 00:10:17,950
Where did they get these intermediate masses?

136
00:10:18,300 --> 00:10:20,550
You can open string type to investigate.

137
00:10:20,550 --> 00:10:24,320
The rest of the masses here will review these in a minute.

138
00:10:24,690 --> 00:10:26,820
Let me finish the example we already started.

139
00:10:27,420 --> 00:10:29,400
Let me get back to the demo file.

140
00:10:30,060 --> 00:10:35,010
And after I created the list, I want to print all elements to console from the new line.

141
00:10:35,460 --> 00:10:40,170
But instead of creating foreach loop, I will use the feature of Stream API.

142
00:10:40,680 --> 00:10:43,960
I create a stream using the variable of modified products.

143
00:10:44,370 --> 00:10:51,390
After that, I call for each method that takes consumer as an argument and I pass mast reference here

144
00:10:51,870 --> 00:10:54,110
out property of a system class.

145
00:10:54,110 --> 00:10:59,240
His method printer lan that we use very often during the bargain of our programs.

146
00:10:59,640 --> 00:11:05,430
So I just use reference to this massive let me run the program to prove you that it works.

147
00:11:05,970 --> 00:11:07,100
And here we go.

148
00:11:07,320 --> 00:11:11,040
You can see that I have only three products with modified price.

149
00:11:11,550 --> 00:11:12,140
Awesome.

150
00:11:12,600 --> 00:11:14,460
Everything works as expected.

151
00:11:15,270 --> 00:11:19,310
Now let me reproduce the second example from our presentation.

152
00:11:19,650 --> 00:11:22,290
Imagine that we have multiple warehouses.

153
00:11:22,530 --> 00:11:25,170
I create a few warehouses, objects here.

154
00:11:25,500 --> 00:11:27,800
Each warehouse has lists of products.

155
00:11:28,290 --> 00:11:32,850
That's why I add a different list of objects to the warehouses here.

156
00:11:33,270 --> 00:11:39,450
And imagine we have to iterate over all products to decrease price only for products that have more

157
00:11:39,450 --> 00:11:41,040
than 1000 items.

158
00:11:41,640 --> 00:11:46,010
First of all, I group all my warehouse objects into one list.

159
00:11:46,500 --> 00:11:49,290
Now I create a stream out of this list.

160
00:11:49,770 --> 00:11:52,700
After that I call flat my math.

161
00:11:53,130 --> 00:11:54,120
What is it for?

162
00:11:54,570 --> 00:12:02,610
This method exists to extract elements from NASA once this method will return stream consistent of the

163
00:12:02,610 --> 00:12:09,900
result of replacing each element of the stream with the contents of a mapped stream produced by applying

164
00:12:09,900 --> 00:12:13,040
the provided map and function to each element.

165
00:12:13,740 --> 00:12:22,170
Is it clear will path function to this method that will produce stream of elements from each element?

166
00:12:22,800 --> 00:12:29,610
In our particular case, we have to get products from each warehouse object and create a stream on list

167
00:12:29,610 --> 00:12:30,240
of products.

168
00:12:30,780 --> 00:12:38,040
Before we call this flat map method, we have conveyor belt of warehouse elements and after we called

169
00:12:38,040 --> 00:12:41,360
flat map, we have stream of product elements.

170
00:12:41,760 --> 00:12:46,030
Can you understand now how we can transform data in our stream?

171
00:12:46,830 --> 00:12:51,010
After that, we are filtering and applying function to all elements.

172
00:12:51,570 --> 00:12:53,620
Now let me show you one more thing.

173
00:12:54,060 --> 00:13:00,270
Imagine that according to some massive contracts requirements, you have to reach an array we can call

174
00:13:00,270 --> 00:13:02,670
to array Massud instead of collect.

175
00:13:03,120 --> 00:13:07,830
But in case we would just call Thouret Array method, we would get array of objects.

176
00:13:08,130 --> 00:13:14,750
What to do in this case, I have to pass the Massud that will create array of the specific types that

177
00:13:14,760 --> 00:13:15,330
I need.

178
00:13:15,900 --> 00:13:19,910
What method can create an array of product constructor?

179
00:13:20,430 --> 00:13:28,020
That's why I specify type here array of product and positive reference to a constructor according to

180
00:13:28,020 --> 00:13:29,250
syntax requirements.

181
00:13:29,250 --> 00:13:31,470
I have to write the new keyword here.

182
00:13:32,040 --> 00:13:34,280
Can you understand now what this meant?

183
00:13:35,160 --> 00:13:38,990
Now Java can create object of product array.

184
00:13:39,720 --> 00:13:43,860
After that I want to print all elements to console from new line.

185
00:13:44,250 --> 00:13:48,260
But we have an array this time can create stream from array.

186
00:13:48,840 --> 00:13:49,820
Yes I can.

187
00:13:50,010 --> 00:13:54,890
I have to call stream Massud from arrays class and parse array of products there.

188
00:13:55,560 --> 00:13:59,180
That's how easily you can create three from any array.

189
00:13:59,940 --> 00:14:06,390
Also during the discussion of functional interfaces, I promise to show you one method and how it might

190
00:14:06,390 --> 00:14:07,050
be helpful.

191
00:14:07,050 --> 00:14:12,540
While working with Stream API, I'm talking about identity Masset from function type.

192
00:14:13,140 --> 00:14:19,050
Imagine that you want to convert list of products to the map type where you're going to have product

193
00:14:19,050 --> 00:14:23,850
name as a key and product type as a value that is mapped to this key.

194
00:14:24,150 --> 00:14:25,230
How to do that.

195
00:14:25,800 --> 00:14:30,630
You'll remember at the beginning of the lesson I created products placed in this example.

196
00:14:30,630 --> 00:14:35,550
I use the same list of products, I create stream code, collect method.

197
00:14:35,560 --> 00:14:40,770
And after that I called to map Masset from collectors' class and past two functions.

198
00:14:41,160 --> 00:14:47,730
The first one should create keys, and the second one is a function that can create product values that

199
00:14:47,730 --> 00:14:49,920
are associated with a specific case.

200
00:14:50,460 --> 00:14:56,010
Taking into account, I don't need to modify a product, I should return the same product.

201
00:14:56,430 --> 00:15:00,210
That's why I use identity MASSATA function type here.

202
00:15:00,930 --> 00:15:08,520
Identity MassArt returns the same object, does it make sense, the last good example that I want to

203
00:15:08,520 --> 00:15:15,750
share with you is map to end and some that I would even say that a group of mass that you will use and

204
00:15:15,750 --> 00:15:17,840
these two are just one of many.

205
00:15:18,360 --> 00:15:23,820
There are numerous of business cases when they need to process collection of elements and find is a

206
00:15:23,820 --> 00:15:28,560
max price or some old values of some field or something similar.

207
00:15:29,040 --> 00:15:35,760
To perform these operations, we have to convert our regular stream to in stream or double stream along

208
00:15:35,760 --> 00:15:36,160
stream.

209
00:15:36,720 --> 00:15:44,640
That's why Stream Type has such masses as map to end, map to double mapped along this massive stage

210
00:15:44,640 --> 00:15:52,020
function as an argument then can extract some no value out of current item in stream and these masses

211
00:15:52,020 --> 00:15:56,760
rich on specific stream type like any stream that will stream along stream.

212
00:15:57,300 --> 00:15:59,760
These types declare new methods.

213
00:16:00,300 --> 00:16:06,000
For example, imagine the case when you need to calculate total amount of items of all products in the

214
00:16:06,000 --> 00:16:08,100
warehouse aggregate stream.

215
00:16:08,100 --> 00:16:14,940
And after that cool map to Inmarsat and parse function that returns into value based on each product,

216
00:16:15,480 --> 00:16:16,430
is that clear?

217
00:16:17,070 --> 00:16:20,130
And after that I can call some method.

218
00:16:20,610 --> 00:16:27,870
This mass at will sum all integers in my end stream that I received after map to Inmarsat invocation.

219
00:16:28,380 --> 00:16:35,570
And here I bring the total items amount to consult, by the way, instead of some you can call Max mean

220
00:16:35,580 --> 00:16:38,160
average or any other methods you would like.

221
00:16:38,960 --> 00:16:45,110
Myside examples from this file, I also prepared a lot of other examples for you, which are the masses,

222
00:16:45,380 --> 00:16:48,500
I believe, linked to this file in attachment to this lesson.

223
00:16:49,070 --> 00:16:54,830
I want you to investigate these good examples by yourself after the lesson, because technical is a

224
00:16:54,830 --> 00:16:56,300
syntax is the same.

225
00:16:56,300 --> 00:16:58,280
And you saw already a few examples.

226
00:16:58,670 --> 00:17:01,270
Some of the methods are used very rare.

227
00:17:01,580 --> 00:17:03,590
Some of them are used more often.

228
00:17:03,870 --> 00:17:06,020
We'll talk about the masses in a minute.

229
00:17:06,500 --> 00:17:12,200
But remember that you can find any good example with any message from Stream API by searching through

230
00:17:12,200 --> 00:17:12,840
this file.

231
00:17:13,490 --> 00:17:19,400
Now, when you saw how you can use the masses and stream API, it is only a matter of what that you

232
00:17:19,400 --> 00:17:20,000
want to call.

233
00:17:20,360 --> 00:17:24,590
We cover all aspects of using stream API from the syntax standpoint.

234
00:17:24,980 --> 00:17:29,650
Now we have to learn the masses that you might want to use while working with streams.

235
00:17:30,050 --> 00:17:32,720
I want to show you a cheat sheet, I call it.

236
00:17:32,730 --> 00:17:37,050
So was the main operations with streams and shared examples.

237
00:17:37,430 --> 00:17:42,710
Remember, you can use a cheat sheet in case you forgot something on this slide.

238
00:17:42,710 --> 00:17:45,380
You can see how you can create a stream with examples.

239
00:17:45,740 --> 00:17:47,530
Some of them we already reviewed.

240
00:17:47,870 --> 00:17:50,890
I'm talking about creating a stream from collections and from array.

241
00:17:51,290 --> 00:17:52,910
Let's review other options.

242
00:17:53,600 --> 00:17:56,180
We can create stream of elements if we want.

243
00:17:56,480 --> 00:17:59,540
We can create stream of lines in case we read file.

244
00:18:00,020 --> 00:18:02,570
We will learn more about this during the topic.

245
00:18:02,570 --> 00:18:04,470
Input output streams in Java.

246
00:18:05,060 --> 00:18:08,510
Also, as you can see here, we can create a stream from stream.

247
00:18:08,930 --> 00:18:11,540
We can use Stream Builder to add elements.

248
00:18:11,540 --> 00:18:18,620
And after that stream also you can invoke parallel stream that would process all elements in different

249
00:18:18,620 --> 00:18:20,150
threats of execution.

250
00:18:20,600 --> 00:18:26,840
Ensured parallel stream will not always bring you better performance because coordination of multiple

251
00:18:26,840 --> 00:18:30,340
threats of execution also requires some calculations.

252
00:18:30,770 --> 00:18:35,060
That's why use parallel stream only with the amount of data.

253
00:18:35,420 --> 00:18:41,780
It is hard to see what should be considered as Beke because it will depend on the operations you are

254
00:18:41,780 --> 00:18:43,370
going to perform and stream.

255
00:18:43,580 --> 00:18:49,250
It will also depends on the type of the object that you are going to use and how big it is.

256
00:18:49,640 --> 00:18:56,420
I would even say that in case you are not sure whether the parallel stream in some particular case always

257
00:18:56,420 --> 00:18:58,940
opt for regular stream in such cases.

258
00:18:59,600 --> 00:19:05,930
Also, we can create infinite sequential order stream produced by iterative application of a function

259
00:19:05,930 --> 00:19:07,490
to an initial element.

260
00:19:08,030 --> 00:19:14,030
Besides that, we can create an infinite sequential stream where each element is generated by the provided

261
00:19:14,030 --> 00:19:14,640
supply.

262
00:19:15,170 --> 00:19:19,950
In other words, we can pass a function that can generate elements and create a stream.

263
00:19:20,480 --> 00:19:24,080
To be honest, I didn't use the last two options very often.

264
00:19:24,860 --> 00:19:28,330
So now you know all possible ways to create stream.

265
00:19:28,700 --> 00:19:29,540
Let's move on.

266
00:19:30,200 --> 00:19:33,290
On this slide, you can see different intermediate methods.

267
00:19:33,620 --> 00:19:38,000
Some of them looks familiar to you because we had examples with those masses.

268
00:19:38,420 --> 00:19:45,430
You already know when you might want to use filter, map, map to end flatman methods, don't you?

269
00:19:45,950 --> 00:19:52,940
You also can use skip method to skip specific number of elements you can call distant Masad to get stream

270
00:19:52,940 --> 00:19:59,480
without duplication in case you want to apply consumer function to each element you can call peak MassArt.

271
00:19:59,990 --> 00:20:05,510
Usually it is used for debugging purposes, the print element to console to investigate the state of

272
00:20:05,510 --> 00:20:07,100
a stream lamet.

273
00:20:07,100 --> 00:20:11,830
Masset allows you to limit the number of elements in your stream source.

274
00:20:12,060 --> 00:20:16,250
It allows us to source elements in stream by natural ordering.

275
00:20:16,640 --> 00:20:22,610
There is also overloaded version of this method that takes Comparator as an argument to define the source

276
00:20:22,610 --> 00:20:23,120
and order.

277
00:20:23,720 --> 00:20:28,850
Using these intermediate operations, you can do amazing things with data manipulation.

278
00:20:29,300 --> 00:20:35,150
Let's look at the next slide and on this slide you can find a list of terminal operations.

279
00:20:35,570 --> 00:20:42,350
Today during the demo, you saw collect Masset for each entero find first method returns, the first

280
00:20:42,350 --> 00:20:43,310
element from stream.

281
00:20:43,970 --> 00:20:49,430
This might be useful in case your source elements in stream and want to take the first elements after

282
00:20:49,430 --> 00:20:57,290
sorting, find any MACIT returns, any element from stream patterns that these two methods return object

283
00:20:57,290 --> 00:20:58,330
of type optional.

284
00:20:58,820 --> 00:21:05,570
This is not the lesson about optional, but ensured optional is a type that is used as a container for

285
00:21:05,570 --> 00:21:10,010
objects that may or may not contain and nonono value.

286
00:21:10,400 --> 00:21:17,480
Also, these types introduce API to extract value from container or perform as operations in case a

287
00:21:17,480 --> 00:21:18,770
container is empty.

288
00:21:19,580 --> 00:21:27,410
We can call Count Massud in case we want to get a number of elements in this stream and any match non-match

289
00:21:27,410 --> 00:21:30,740
and all march methods take predicate to return.

290
00:21:30,740 --> 00:21:31,850
True or false?

291
00:21:32,450 --> 00:21:38,510
I believe that it is clear from the method names that any match will return true in case at least one.

292
00:21:38,690 --> 00:21:45,380
The elements from streamy, the conditions of predicates that we passed to this massive non-match will

293
00:21:45,380 --> 00:21:52,730
return true in case non of elements Margitza Predicate and all March will return true only in case all

294
00:21:52,730 --> 00:21:56,890
elements march pre-cut main and maximizers.

295
00:21:56,900 --> 00:22:03,620
I believe these are self describing this massive state comparator as an argument for each order.

296
00:22:03,620 --> 00:22:09,560
It must is similar to for each Massett, but it also supports order of elements in the stream.

297
00:22:10,370 --> 00:22:16,370
Reduce mass performs reduction stream operations that allows us to produce one single result from a

298
00:22:16,370 --> 00:22:21,310
sequence of elements in this operation usually take part three participants.

299
00:22:21,530 --> 00:22:23,930
They are a regional element.

300
00:22:24,200 --> 00:22:26,150
It is also called identity.

301
00:22:26,720 --> 00:22:29,300
The second participant is an accumulator.

302
00:22:29,660 --> 00:22:36,110
This is a function that takes two parameters, a partial result of the reduction operation and the next

303
00:22:36,110 --> 00:22:37,250
element of the string.

304
00:22:37,760 --> 00:22:40,030
And the third participant is a combiner.

305
00:22:40,250 --> 00:22:46,310
This is a function that is used to combine the partial result of the reduction operation once the reduction

306
00:22:46,310 --> 00:22:52,550
is paralyzed or when there is a mismatch between the types of the accumulator arguments and the types

307
00:22:52,550 --> 00:22:54,500
of the accumulator implementation.

308
00:22:55,040 --> 00:23:01,200
And I know that this can sound not clear, but don't worry about that after this lesson.

309
00:23:01,220 --> 00:23:03,680
Take your time to investigate the examples.

310
00:23:04,280 --> 00:23:10,570
I didn't show you this example because to be honest, this function is not used super often, really.

311
00:23:11,270 --> 00:23:16,490
So take your time to investigate examples and feel free to ask questions in case you have any.

312
00:23:17,210 --> 00:23:19,550
That's all what I wanted to share with you today.

313
00:23:19,790 --> 00:23:24,950
Now let's recap what we have learned today so that we learned really a lot of things.

314
00:23:25,220 --> 00:23:26,870
But let's try to sum it up.

315
00:23:27,320 --> 00:23:30,560
We understand what Stream API is now.

316
00:23:30,560 --> 00:23:36,770
We know how to create stream, how to use intermediate operations and how to use terminal operations

317
00:23:37,130 --> 00:23:38,420
on real life examples.

318
00:23:38,420 --> 00:23:41,540
You saw how you can apply your knowledge about Stream API.

319
00:23:41,960 --> 00:23:47,080
Also, you got a lot of examples that you can use during the development when you need.

320
00:23:47,660 --> 00:23:49,730
And now let me show your homework.

321
00:23:50,540 --> 00:23:52,700
You can find multiple tasks here.

322
00:23:53,180 --> 00:23:59,840
The important thing in each task is to implement those with the help of Stream API and functional interfaces.

323
00:24:00,260 --> 00:24:06,110
You can practice a lot in writing your own lambda expressions and then processing elements in stream

324
00:24:06,740 --> 00:24:08,240
read tasks attentively.

325
00:24:08,660 --> 00:24:15,650
I edit as much details as I could in each task and remember, in case you have any question gunshy to

326
00:24:15,650 --> 00:24:17,260
ask me that.

327
00:24:17,280 --> 00:24:20,750
So what I have for you for today, thanks a lot for your attention.

328
00:24:20,900 --> 00:24:23,990
Have a great day and see you in the next lesson.
