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