1
00:00:00,330 --> 00:00:06,450
We're going to be performing some really long operations that depend on a data set that has roughly

2
00:00:06,450 --> 00:00:07,980
one million records.

3
00:00:08,250 --> 00:00:12,480
So you already know whatever operations we're going to make, they're going to be really long and they're

4
00:00:12,480 --> 00:00:14,350
definitely not going to be on the main thread.

5
00:00:14,370 --> 00:00:15,900
Otherwise, you're going to be blocking it.

6
00:00:16,770 --> 00:00:21,180
OK, if you go back to the application, you'll notice the main threat already has a line of code that

7
00:00:21,180 --> 00:00:23,700
grabs the path of as v file.

8
00:00:24,270 --> 00:00:29,730
And using this path object, what we can do is run through every line in the CSV file as a stream of

9
00:00:29,730 --> 00:00:30,420
elements.

10
00:00:31,410 --> 00:00:34,740
So back to mean not Java, don't show again.

11
00:00:37,710 --> 00:00:39,510
Well, so by creating the average function.

12
00:00:40,520 --> 00:00:45,350
Public static seems that the function expects or returns a double.

13
00:00:46,940 --> 00:00:50,780
It's called average, it expects a path variable.

14
00:00:51,980 --> 00:00:52,820
Named Path.

15
00:00:53,740 --> 00:00:56,200
As well as string variable named category.

16
00:00:59,650 --> 00:01:03,280
And inside the function, the first thing we're going to do is a run through every line from this year's

17
00:01:03,280 --> 00:01:04,959
V file as a stream of elements.

18
00:01:05,319 --> 00:01:09,100
I left you a hint inside the article and that was to use the files class.

19
00:01:11,480 --> 00:01:16,340
Pilots without lines inside, we pass in the path to our skies, we file.

20
00:01:20,500 --> 00:01:25,270
And if you look at the documentation, this reads all lines from a file as a stream of elements.

21
00:01:26,260 --> 00:01:31,000
And it seems like Lines throws a checked exception, so what we're going to have to do is try to run

22
00:01:31,000 --> 00:01:31,450
the code.

23
00:01:33,440 --> 00:01:39,230
Before we start our stream pipeline and catch the exception, if the code fails, catch IO exception

24
00:01:39,410 --> 00:01:40,250
is what it was.

25
00:01:43,710 --> 00:01:46,920
And will perform some logic here later.

26
00:01:47,640 --> 00:01:52,740
First thing we're going to do is as we're running through this stream of elements, we want to skip

27
00:01:53,520 --> 00:01:54,570
the very first line.

28
00:01:55,470 --> 00:02:02,160
Because you'll notice and let me just scroll up, you'll notice the very first line is just a series

29
00:02:02,160 --> 00:02:06,780
of column headers, category price of item in each category and quantity of items sold.

30
00:02:08,020 --> 00:02:10,870
The way we can do that is say, don't skip.

31
00:02:12,860 --> 00:02:16,250
And inside, you want to specify the element in the stream that you want to skip.

32
00:02:16,580 --> 00:02:18,860
We want to skip the very first elements.

33
00:02:19,580 --> 00:02:20,270
Skip one.

34
00:02:24,080 --> 00:02:28,900
Now, the next thing we want to do is we want to map every single element in this dream to an array

35
00:02:28,900 --> 00:02:30,190
of three string values.

36
00:02:30,550 --> 00:02:34,220
So we're going to map every single element in this dream.

37
00:02:34,240 --> 00:02:40,030
Remember, here we have to specify a lambda expression that receives every single element, which is

38
00:02:40,030 --> 00:02:40,780
just a line.

39
00:02:41,760 --> 00:02:46,890
We always have the arrow that points to a value we want to return, and we're going to map every single

40
00:02:46,890 --> 00:02:54,660
line to an array of three string values line dot split and we're going to split every element into an

41
00:02:54,660 --> 00:02:57,180
array of three strings separated by a comma.

42
00:03:00,010 --> 00:03:05,050
OK, now every element in the stream should be an array of three values.

43
00:03:09,740 --> 00:03:13,190
And so now what we want to do is we want to filter the stream by category.

44
00:03:13,490 --> 00:03:20,330
So if the user were to pass any category of furniture, we want to filter the stream to only have furniture

45
00:03:20,330 --> 00:03:20,990
elements.

46
00:03:21,680 --> 00:03:25,130
So what we can do is say, don't show again, this keeps popping up.

47
00:03:25,880 --> 00:03:30,350
Don't Filter Filter is going to receive an array of three string values.

48
00:03:31,540 --> 00:03:34,810
And we want to filter the elements whose values.

49
00:03:36,760 --> 00:03:37,990
At Index Zero.

50
00:03:40,410 --> 00:03:42,480
Equals the category that gets passed in.

51
00:03:46,210 --> 00:03:51,430
And now we want to do is map every element in the stream to a double price times quantity.

52
00:03:53,370 --> 00:03:58,350
So now what we want to do is map every single element in the updated stream to a sales value.

53
00:03:58,650 --> 00:04:03,960
So we're going to say map to double because we expect to map every single element in the stream to a

54
00:04:03,960 --> 00:04:04,650
double value.

55
00:04:05,250 --> 00:04:10,500
And we're going to map every single element, which is currently an array of three string values to

56
00:04:10,500 --> 00:04:12,270
a double value by multiplying.

57
00:04:13,320 --> 00:04:18,450
The value at index one, let me just make sure of that, yet the value of the index one times the value

58
00:04:18,450 --> 00:04:19,260
at index to.

59
00:04:20,300 --> 00:04:21,079
Times.

60
00:04:22,870 --> 00:04:23,900
Let's me remove that.

61
00:04:25,090 --> 00:04:26,800
Times the value at index to.

62
00:04:29,440 --> 00:04:29,830
OK.

63
00:04:30,550 --> 00:04:35,500
And now every single element in the stream should now be a double value to that stream, we're going

64
00:04:35,500 --> 00:04:37,990
to apply a terminal operation called average.

65
00:04:38,260 --> 00:04:42,400
And what it's going to do is basically take the average of every single element in the stream.

66
00:04:43,120 --> 00:04:46,750
But average returns an optional double.

67
00:04:47,320 --> 00:04:51,250
So we're going to use get as double to return a concrete double.

68
00:04:52,800 --> 00:04:53,340
And that's it.

69
00:04:54,030 --> 00:04:57,990
And now what we want to do is return the results of this dream pipeline.

70
00:04:58,950 --> 00:05:04,170
We do get an error here, and that's because our expression doesn't know that each element in values

71
00:05:04,170 --> 00:05:05,730
is actually a quantity.

72
00:05:06,060 --> 00:05:07,460
It interprets them as a string.

73
00:05:07,470 --> 00:05:10,860
So what we can do is, say, double the value of.

74
00:05:12,520 --> 00:05:13,330
Values one.

75
00:05:14,690 --> 00:05:21,410
We're passing a double from each string value times, double the value of values to.

76
00:05:23,950 --> 00:05:30,100
And we should be good, and now we get another compilation error, because in the event of an exception,

77
00:05:30,100 --> 00:05:34,540
catch is going to get called, in which case we still need to return some sort of value.

78
00:05:34,840 --> 00:05:40,690
So for now, we'll just print the message that comes out of this exception, exception get message and

79
00:05:40,690 --> 00:05:44,420
then we'll just return zero if the user gets back zero.

80
00:05:44,440 --> 00:05:48,160
It's a pretty clear indication that something's wrong and there should be it.

81
00:05:50,030 --> 00:05:55,940
And it seems like for a task to we need to create a very similar function where this function takes

82
00:05:55,940 --> 00:05:58,640
the average sales for one category.

83
00:05:58,670 --> 00:06:02,650
This function takes the total average across the entire sets.

84
00:06:03,020 --> 00:06:09,080
So in very similar fashion, we'll create a public static method that returns a double.

85
00:06:09,350 --> 00:06:11,060
This one is called total average.

86
00:06:11,930 --> 00:06:13,700
It receives a path object.

87
00:06:16,420 --> 00:06:22,390
And once again, we will try to run the files, the lines code.

88
00:06:23,930 --> 00:06:27,050
And in the event of an error, we're going to catch the oh exception.

89
00:06:30,000 --> 00:06:31,920
In which case we're going to print system.

90
00:06:31,950 --> 00:06:37,530
I doubt that print line inkjet message and return zero point zero.

91
00:06:38,510 --> 00:06:42,770
OK, and in this case, once again, here we're running through every single line from this year's V

92
00:06:42,770 --> 00:06:43,550
file as a stream.

93
00:06:44,030 --> 00:06:47,870
First, we map every single element to an array of three values.

94
00:06:48,170 --> 00:06:51,170
So we're going to map every single line.

95
00:06:52,700 --> 00:06:59,180
Every single element to an array of three string values, line darts split.

96
00:07:00,410 --> 00:07:02,660
And we're going to split values around a comma.

97
00:07:04,220 --> 00:07:08,870
There is no need to filter anything here, we can just directly map every single element in this dream

98
00:07:08,870 --> 00:07:09,830
to a double value.

99
00:07:10,550 --> 00:07:16,340
So the lambda expression is going to receive an array of three values and we're going to map every single

100
00:07:16,340 --> 00:07:21,770
element to the multiplication of values one times values to.

101
00:07:23,820 --> 00:07:30,150
But you can't multiply string value, so what we need to do is pass a double from each string.

102
00:07:40,890 --> 00:07:44,340
And now what we want to do is apply the terminal operation average.

103
00:07:46,090 --> 00:07:47,680
In return, the average double.

104
00:07:50,010 --> 00:07:50,400
OK.

105
00:07:50,670 --> 00:07:53,310
Don't forget to return the result of the stream pipeline.

106
00:07:53,640 --> 00:07:55,310
And it seems like we're good.

107
00:07:56,880 --> 00:07:59,430
OK, let me just double check everything, make sure it's good.

108
00:07:59,880 --> 00:08:04,190
First, we're skipping the very oh, we forgot something.

109
00:08:05,230 --> 00:08:10,570
We need to skip the very first line because there is nothing that we can do with it.

110
00:08:11,870 --> 00:08:13,850
And I think now we should be good.

111
00:08:14,870 --> 00:08:18,650
Now inside men, I'm going to call the average method for furniture.

112
00:08:20,430 --> 00:08:24,920
I'm not going to print anything, I'll just say average path furniture.

113
00:08:27,940 --> 00:08:32,049
I'll copy and paste this two more times, I'll take the average very technology.

114
00:08:33,039 --> 00:08:36,340
The average for office supplies.

115
00:08:37,530 --> 00:08:40,140
And then I can calculate the total average.

116
00:08:41,250 --> 00:08:43,559
By just reading every single line from the path.

117
00:08:45,000 --> 00:08:48,360
And after we call all of our methods, we need to copy some code into men.

118
00:08:50,000 --> 00:08:53,150
I left you this code inside, learn the parts, so make sure to copy it.

119
00:08:56,170 --> 00:09:04,270
And so the idea is that when the user first starts interacting with our app, they're expecting to start

120
00:09:04,270 --> 00:09:05,560
interacting with it right away.

121
00:09:05,920 --> 00:09:12,370
But these tasks take so long to execute that they're going to block the main thread and basically ruin

122
00:09:12,370 --> 00:09:13,450
the user experience.

123
00:09:13,810 --> 00:09:15,070
So let's see that in action.

124
00:09:15,340 --> 00:09:16,390
We're going to run the code.

125
00:09:20,050 --> 00:09:22,690
And wow, this was really bad user experience.

126
00:09:22,990 --> 00:09:27,310
The user shouldn't have to wait so long before they can answer the question, and that is why we need

127
00:09:27,310 --> 00:09:32,890
to ensure that long running tasks do not block the main threat because blocking the main thread negatively

128
00:09:32,890 --> 00:09:34,360
affects user experience.

129
00:09:34,630 --> 00:09:37,520
That's something you have to keep in mind as you're building apps in the future.

130
00:09:38,110 --> 00:09:41,890
And so we can conclude that these calculations are time intensive.

131
00:09:42,280 --> 00:09:45,400
So what we're going to do is run each calculation on another threat.

132
00:09:46,060 --> 00:09:48,250
So remember, we can just.

133
00:09:49,480 --> 00:09:56,140
Create three threat objects thread threat two is equal to a new object of the thread class that expects

134
00:09:56,140 --> 00:09:56,860
a runnable.

135
00:10:06,320 --> 00:10:13,850
We can copy and paste that two more times at three thread for here will perform the task for a technology

136
00:10:14,780 --> 00:10:17,120
will perform the task for office supplies.

137
00:10:19,100 --> 00:10:22,040
And then our last thread will perform the task.

138
00:10:23,780 --> 00:10:26,540
Of calculating the total average will remove that.

139
00:10:28,980 --> 00:10:29,430
That's it.

140
00:10:31,340 --> 00:10:32,480
Now we can call.

141
00:10:34,540 --> 00:10:35,950
Threat to not start.

142
00:10:37,670 --> 00:10:39,260
Threat three starts.

143
00:10:44,160 --> 00:10:48,450
Pretty tedious, but don't worry, in the future, I will show you a much better way of running stuff

144
00:10:48,450 --> 00:10:49,290
on the background.

145
00:10:49,890 --> 00:10:51,030
In the background, I should say.

146
00:10:51,950 --> 00:10:53,480
And if we run the app now.

147
00:10:56,610 --> 00:10:58,350
So enough to rerun it.

148
00:11:00,730 --> 00:11:05,590
And this is beautiful, because as the user is answering this question, these tasks are executing in

149
00:11:05,590 --> 00:11:06,280
the background.

150
00:11:06,910 --> 00:11:09,040
So by the time they actually answer, it's.

151
00:11:10,670 --> 00:11:12,600
The user experience is seamless.

152
00:11:12,620 --> 00:11:17,370
These tasks have already finished running, and that is why you never blocked the main thread.

153
00:11:17,390 --> 00:11:20,630
You make sure that long running tasks execute in the background.

