1 00:00:06,140 --> 00:00:06,480 Hello. 2 00:00:06,690 --> 00:00:12,590 Students, in this lesson, we're going to learn what consumer by consumer product and by product interfaces 3 00:00:12,590 --> 00:00:13,850 are as usual. 4 00:00:13,860 --> 00:00:17,720 We'll start from an investigation of a source code of all interfaces. 5 00:00:17,960 --> 00:00:20,540 We'll review all matters that are defined there. 6 00:00:20,870 --> 00:00:26,450 One real example will show you how you can use these types and how to implement LAMDA functions and 7 00:00:26,450 --> 00:00:31,820 methods, references to implement consumer by consumer product and by product types. 8 00:00:32,330 --> 00:00:34,900 After that, we'll talk about two other types. 9 00:00:35,390 --> 00:00:39,370 We are also going to default and static methods from these interfaces. 10 00:00:39,560 --> 00:00:41,780 We have a lot of things to learn today. 11 00:00:42,040 --> 00:00:42,800 Let's start. 12 00:00:43,550 --> 00:00:48,020 The best way to learn this topic is to investigate the source code and the real examples. 13 00:00:48,290 --> 00:00:53,000 I suggest starting our lesson today from the overview of consumer interface source code. 14 00:00:53,570 --> 00:00:59,180 As you can see, similar to other types declared in Geritol function package consumer is a functional 15 00:00:59,180 --> 00:01:06,120 interface that has one abstract method with a name, except it takes some argument and returns nothing. 16 00:01:06,350 --> 00:01:12,230 So this type exists to perform an operation on the given argument, unlike other functions. 17 00:01:12,260 --> 00:01:15,500 This type of function usually produces side effect. 18 00:01:15,950 --> 00:01:17,090 I'm talking about that. 19 00:01:17,090 --> 00:01:22,670 The reason to use this type is to modify the state of the argument and create a function for that. 20 00:01:23,270 --> 00:01:25,950 This interface also has one default method. 21 00:01:25,970 --> 00:01:31,810 This is and then Masset, in the previous lesson we learned how we can use and then MassArt to compose 22 00:01:31,820 --> 00:01:34,730 new function on the basis of two other functions. 23 00:01:35,120 --> 00:01:37,210 It works the same way with consumer. 24 00:01:37,700 --> 00:01:40,670 That's why it will not pay a lot of attention to this method. 25 00:01:40,910 --> 00:01:46,400 Take into account we reviewed it in previous lesson, but instead I will show you examples of consumer 26 00:01:46,420 --> 00:01:47,180 unpracticed. 27 00:01:47,720 --> 00:01:52,520 I prepared real life example for you that will help you to grasp new knowledge faster. 28 00:01:52,820 --> 00:01:55,610 I will leave the link to this file in the lesson attachments. 29 00:01:56,150 --> 00:01:58,070 Imagine that you have a list of products. 30 00:01:58,480 --> 00:02:01,820 This is product type that we used for some of the home tasks. 31 00:02:01,820 --> 00:02:07,940 While we are in collections, let me remind you how it looks like this typesetters contract that allows 32 00:02:07,940 --> 00:02:13,520 us to retrieve Idy product, name, category, name of each product and set price if needed. 33 00:02:14,120 --> 00:02:20,090 In our example, we will use default product type that implements product interface NASSAN special in 34 00:02:20,090 --> 00:02:21,320 the source code of default. 35 00:02:21,320 --> 00:02:24,470 Product type just data was getters and setters. 36 00:02:25,040 --> 00:02:26,030 And our example. 37 00:02:26,030 --> 00:02:27,710 I created a few test products. 38 00:02:28,160 --> 00:02:32,570 I passed the constructor iji product name, category, name and price. 39 00:02:32,960 --> 00:02:34,460 Hobbs's is clear for you. 40 00:02:35,240 --> 00:02:40,310 Now imagine that I want to give possibility for administrator to increase price of products. 41 00:02:40,610 --> 00:02:43,850 For example, administrator could select the specific products. 42 00:02:44,090 --> 00:02:50,030 Those products will be grouped in the least object on the backend and price will be increased for each 43 00:02:50,030 --> 00:02:53,690 product from this list by amount of money set by admin. 44 00:02:54,380 --> 00:03:00,290 Here, a graded method increase price for product list that takes as arguments and list of products 45 00:03:00,290 --> 00:03:04,980 and specific amount of dollars to increase price and a massive body attack. 46 00:03:05,000 --> 00:03:08,510 Iterator of my product list and call for each method. 47 00:03:08,870 --> 00:03:12,980 Let me open the source code of this method to explore it together with you. 48 00:03:13,460 --> 00:03:17,630 As you can see, this is the default method declared in iterator type. 49 00:03:18,170 --> 00:03:21,890 It takes consumer as an argument in simple words. 50 00:03:21,890 --> 00:03:23,350 It takes some actions. 51 00:03:23,360 --> 00:03:27,530 It has to be performed with each element remained in this iterator. 52 00:03:28,160 --> 00:03:34,430 You can see that we have while loop here and accept method of consumer is invoked for each element from 53 00:03:34,430 --> 00:03:35,480 this iterator. 54 00:03:35,960 --> 00:03:41,450 Now let's get back to our demo file and we will implement lambda function for our consumer. 55 00:03:41,900 --> 00:03:46,610 Here you can see the anonymous function that takes one argument and that returns nassan. 56 00:03:47,150 --> 00:03:54,710 I call set price Masset on each product and set the result of radiation of current price and local variables 57 00:03:54,710 --> 00:03:56,150 that passed to the Massett. 58 00:03:56,630 --> 00:03:57,830 And then my program. 59 00:03:57,980 --> 00:04:03,170 I can call this method and pass the list of products an amount of money to increase price. 60 00:04:03,800 --> 00:04:07,190 After that, let me print each product from the new line. 61 00:04:07,670 --> 00:04:09,770 I ran the program here. 62 00:04:09,770 --> 00:04:14,600 You can see in console output that price is increased by ten dollars for each product. 63 00:04:15,170 --> 00:04:21,110 Can you understand now how we modified state for all products in connection with the help of consumer 64 00:04:21,500 --> 00:04:23,780 that we passed two for each remaining MassArt? 65 00:04:24,260 --> 00:04:30,080 Hope it makes sense, by the way, I believe from the previous Lasley already understood that this lambda 66 00:04:30,080 --> 00:04:33,200 function is interchangeable with MassArt reference. 67 00:04:33,740 --> 00:04:36,350 Please let me know in case there are any questions. 68 00:04:37,040 --> 00:04:41,720 When you understood how consumer works, let me show you how my consumer works. 69 00:04:41,990 --> 00:04:45,020 I open source code of BI consumer interface. 70 00:04:45,350 --> 00:04:51,200 We learned with you specifics of naming convention individual function package during the previous lesson. 71 00:04:51,740 --> 00:04:58,220 Now you can understand that this by prefix is associated with the Arati of accept function. 72 00:04:58,640 --> 00:05:02,480 You can see that accept takes two arguments and returns nassan. 73 00:05:03,010 --> 00:05:09,510 This interface also declares one default Masset, and then that is similar to other interfaces from 74 00:05:09,510 --> 00:05:14,340 this package to compose new functions on the basis of already existing functions. 75 00:05:15,030 --> 00:05:19,930 Now, I want to get back to our demo file to show you an example for our next demo. 76 00:05:19,950 --> 00:05:22,370 I created an object of type hash map. 77 00:05:22,680 --> 00:05:27,510 It is parametrized by integer and product type imagines a similar case. 78 00:05:27,780 --> 00:05:34,380 I have product ideas that are mapped against the products and I want to have opportunity to update each 79 00:05:34,380 --> 00:05:36,880 entry in map for that purpose. 80 00:05:36,940 --> 00:05:43,560 I have created increased price for product map method, the text map of ideas and products and price 81 00:05:43,560 --> 00:05:44,190 to increase. 82 00:05:44,770 --> 00:05:49,970 Here you can see that I call for each method on my map object and pass by consumers there. 83 00:05:50,370 --> 00:05:53,520 Here's lambda function that takes two arguments and returns. 84 00:05:53,520 --> 00:05:58,560 Nassan, in case I don't need to modify key, I just ignore it. 85 00:05:58,920 --> 00:06:02,690 And you can see the body of this function is similar to the previous one. 86 00:06:03,270 --> 00:06:07,800 The only difference here is that I have access to the key in case I need it. 87 00:06:08,460 --> 00:06:13,590 Now, in my program I can call the method and parse, map, object and price to increase. 88 00:06:14,100 --> 00:06:19,460 After that we print each product from new line in console and then cancel output. 89 00:06:19,470 --> 00:06:25,340 You can see that price is increased knowing the difference between consumer and by consumer. 90 00:06:25,350 --> 00:06:28,640 You can use either of these types during the development. 91 00:06:29,040 --> 00:06:34,610 Also, you saw how to investigate the matter signature to understand what type is expected. 92 00:06:35,160 --> 00:06:41,670 Usually it is obvious what will be passed to the function as arguments, but in case it is not clear, 93 00:06:41,790 --> 00:06:45,380 you can investigate the source code of matter that requires function. 94 00:06:46,020 --> 00:06:52,080 One of the goals of this course is not to teach you all possible things, but to teach you how you can 95 00:06:52,080 --> 00:06:54,090 learn these things by yourself. 96 00:06:54,720 --> 00:06:59,850 Let me show you how you can understand what values will be passed to the anonymous function during the 97 00:06:59,850 --> 00:07:00,440 runtime. 98 00:07:00,990 --> 00:07:05,430 For example, I open the source code for each method in map type. 99 00:07:05,970 --> 00:07:11,490 The first thing I want to know is where access method is invoked on the buy consumer argument. 100 00:07:12,060 --> 00:07:17,550 Here is MassArt invocation and I see that K and V variables are passed. 101 00:07:17,910 --> 00:07:23,130 But what object stands behind K and V variable based on the variables name? 102 00:07:23,130 --> 00:07:26,250 It is clear what key and value are referencing to. 103 00:07:26,550 --> 00:07:32,760 But just in case we can check it, few lines above we can see that K variable is initialized with key 104 00:07:32,760 --> 00:07:37,590 value on each iteration and variable is initialized with the mapped value. 105 00:07:38,130 --> 00:07:41,100 You can see that operation is performed for each entry. 106 00:07:41,430 --> 00:07:45,620 That's how you can easily understand what values will be passed to the function. 107 00:07:46,290 --> 00:07:52,380 OK, now when we learned consumer and by consumer types, I want to learn with your predicate and by 108 00:07:52,380 --> 00:07:53,310 predicate types. 109 00:07:53,730 --> 00:07:58,740 I believe that knowledge of Lambda functions and Massata references will help us to learn predicate 110 00:07:58,770 --> 00:07:59,700 type faster. 111 00:08:00,330 --> 00:08:03,060 Let me open the source code of Predicate Interface. 112 00:08:03,360 --> 00:08:06,540 Prickett has one abstract method with the name test. 113 00:08:07,110 --> 00:08:09,510 This method takes an argument and returns. 114 00:08:09,510 --> 00:08:13,260 Boolean value is a true or false in stream API. 115 00:08:13,260 --> 00:08:16,920 Predicates are used very often to filter values and remove elements. 116 00:08:16,920 --> 00:08:20,400 For example, to help you understand how it works. 117 00:08:20,550 --> 00:08:22,410 Let me show you a simple example. 118 00:08:22,950 --> 00:08:25,410 For pre-cut example, I created a separate file. 119 00:08:25,530 --> 00:08:28,140 I will also leave link to this file in attachments. 120 00:08:28,140 --> 00:08:28,760 Does lesson. 121 00:08:29,340 --> 00:08:31,730 Imagine that you have a list of products here. 122 00:08:31,740 --> 00:08:37,800 You can see product list that are similar to one we have in consumer demo file and we have to remove 123 00:08:37,800 --> 00:08:44,460 all products from this list that have price more than you specified to this requirement I created to 124 00:08:44,460 --> 00:08:47,070 remove products if prices more than Massett. 125 00:08:47,070 --> 00:08:54,030 That takes a list of products and price as arguments on my list object I call remove if MassArt let 126 00:08:54,030 --> 00:08:55,920 me open source code of this method. 127 00:08:56,340 --> 00:09:02,580 As you can see, this method is from collection interface and it takes Birckhead as an argument. 128 00:09:03,150 --> 00:09:09,690 Test method will be invoked for each element in this list and in case that Masset will return true, 129 00:09:10,050 --> 00:09:12,270 then this element will be removed. 130 00:09:12,660 --> 00:09:18,300 Based on this, we can make a conclusion that we need to write a function that will return true for 131 00:09:18,300 --> 00:09:20,790 elements that must be removed. 132 00:09:21,030 --> 00:09:24,660 Is it clear now let's get back to our demo file. 133 00:09:25,140 --> 00:09:27,210 I wrote function here as it returns. 134 00:09:27,210 --> 00:09:32,430 True, in case product price is more than price value that was passed to this method. 135 00:09:32,880 --> 00:09:38,340 And in my program I call the new method and pass list of products and one hundred. 136 00:09:38,950 --> 00:09:43,320 That means that all products that have price more than one hundred will be removed. 137 00:09:43,860 --> 00:09:47,970 We can see that I have only one product with price more than one hundred. 138 00:09:48,400 --> 00:09:54,560 That's why when I print all product to console from the new line here, I still expect to see the rest 139 00:09:54,560 --> 00:09:55,260 two products. 140 00:09:55,680 --> 00:09:58,680 Let me run the program by some console output. 141 00:09:58,680 --> 00:10:02,280 We can make a conclusion that product with price more than one hundred. 142 00:10:02,470 --> 00:10:04,030 Was removed from my list. 143 00:10:04,690 --> 00:10:05,260 Awesome. 144 00:10:06,130 --> 00:10:13,420 Now let's investigate the default and static methods from project interface it has and method that allows 145 00:10:13,420 --> 00:10:16,870 us to compose two functions and add additional predicate. 146 00:10:17,290 --> 00:10:23,470 This gives us a similar flexibility that we learned in lesson about functions when we combine different 147 00:10:23,470 --> 00:10:24,040 functions. 148 00:10:24,550 --> 00:10:27,230 Let me show you the next example in the source code. 149 00:10:28,060 --> 00:10:33,600 Imagine that I need to remove all products that have price more than some specific value and category 150 00:10:33,610 --> 00:10:38,770 name is Category one and create a Masset where I create two functions. 151 00:10:38,930 --> 00:10:41,500 The first one is already familiar to you. 152 00:10:41,890 --> 00:10:47,290 That function is to test the price and the second function checks category. 153 00:10:47,920 --> 00:10:54,910 And when I call remove if MassArt, I create the new function that is returned as a result of and Masset 154 00:10:54,910 --> 00:10:55,750 invocation. 155 00:10:56,140 --> 00:11:04,210 I combine these two predicates now aliment will be removed only in case requirements of two predicates 156 00:11:04,210 --> 00:11:05,050 will be met. 157 00:11:05,590 --> 00:11:10,780 In my program I pass list of products, price and category one name. 158 00:11:11,410 --> 00:11:17,170 Based on the current state of my product list, I can see that only one product will be removed after 159 00:11:17,170 --> 00:11:24,880 the Macit invocation product with i.e. one has price more than 90 and category name is equal to category 160 00:11:24,880 --> 00:11:25,210 one. 161 00:11:25,810 --> 00:11:30,190 And in console output you can see that the product was a new one was removed. 162 00:11:30,850 --> 00:11:36,580 And I want you to understand that you're going to compose multiple predicates together, not only to 163 00:11:36,580 --> 00:11:38,200 but even more if you need. 164 00:11:38,620 --> 00:11:44,410 That will allow you to create customized predicates, one that will be needed in case we want to compose 165 00:11:44,410 --> 00:11:47,110 predicates to receive predicate that will return. 166 00:11:47,110 --> 00:11:54,370 True, in case we meet the requirements of at least one predicate of combined ones that we need to use 167 00:11:54,370 --> 00:11:57,610 default or method from predicate interface. 168 00:11:58,210 --> 00:12:02,560 I believe that is logical and clear and does not require any additional demo. 169 00:12:03,220 --> 00:12:09,910 The next default method and predicate interface is negate it returns logical negation of the current 170 00:12:09,910 --> 00:12:10,480 predicate. 171 00:12:10,780 --> 00:12:11,590 Is it clear? 172 00:12:12,070 --> 00:12:13,870 Let me show you this on example. 173 00:12:14,470 --> 00:12:20,440 In case in our previous example I would call Negate Masset, the result would be opposite. 174 00:12:20,930 --> 00:12:27,670 That means in case this function returns true, as a result, the force will be returned after negation. 175 00:12:28,240 --> 00:12:32,080 Let me call negate method here and run program one more time. 176 00:12:32,680 --> 00:12:37,180 And in console output you can see that product with ADC was removed. 177 00:12:37,810 --> 00:12:44,050 So the product that is less than 90 and the product that has category names that is not equal to Category 178 00:12:44,050 --> 00:12:45,150 one was removed. 179 00:12:45,700 --> 00:12:48,130 Can you understand now what it is? 180 00:12:48,790 --> 00:12:50,590 If yes, then let's proceed. 181 00:12:51,250 --> 00:12:53,050 We have two more static methods. 182 00:12:53,050 --> 00:12:54,460 Here is equal. 183 00:12:54,460 --> 00:13:00,760 MassArt returns use a predicate that will check if two arguments are equal to make sure that predicate 184 00:13:00,760 --> 00:13:07,240 will work as expected, we need to check that equal Masset in the type that we want to use for comparison 185 00:13:07,240 --> 00:13:08,260 is overeaten. 186 00:13:08,770 --> 00:13:15,910 This is needed because for comparison will be used objects equal MassArt and the second method is not. 187 00:13:16,060 --> 00:13:18,130 That takes expected as an argument. 188 00:13:18,610 --> 00:13:21,730 This method negates the result of the supplied predicate. 189 00:13:22,070 --> 00:13:26,710 You can see that this is achieved by using the call of Landgate method inside. 190 00:13:27,190 --> 00:13:32,530 So technically you can use is an agreed method in case you have variable of type predicate. 191 00:13:32,800 --> 00:13:38,500 Like I showed you an example, one in case you don't have variable of type predicate you can call predicate, 192 00:13:38,500 --> 00:13:40,600 not method and pos lambda function. 193 00:13:40,600 --> 00:13:47,950 Here to help you understand these two masses, let me show you an example in our perfect demo file. 194 00:13:47,980 --> 00:13:50,520 We have one more example at the bottom here. 195 00:13:50,830 --> 00:13:57,040 Imagine that we have a list of users I use user for hash tables type that implements user interface. 196 00:13:57,370 --> 00:14:02,200 I use exactly Zeze implementation because it as equals Masset overridden. 197 00:14:02,620 --> 00:14:04,630 We added a few users to the list. 198 00:14:04,990 --> 00:14:09,580 You can see user i.e. first name, last name, password and email. 199 00:14:10,000 --> 00:14:11,740 That's just for the sake of example. 200 00:14:12,070 --> 00:14:17,470 And according to business requirements, I have to be able to filter the list of users to leave their 201 00:14:17,470 --> 00:14:19,810 only user that I need here. 202 00:14:19,810 --> 00:14:22,870 I created a user object that will be used during filtering. 203 00:14:23,410 --> 00:14:28,540 I call remove if MassArt on my list of users and I call Predicate is equal method here. 204 00:14:29,020 --> 00:14:35,200 But in case I would call just this method, I will get protocols that will remove only user that equals 205 00:14:35,200 --> 00:14:35,980 to this one. 206 00:14:36,460 --> 00:14:38,530 I need exactly the opposite. 207 00:14:38,890 --> 00:14:41,080 I need to keep only this user. 208 00:14:41,500 --> 00:14:44,170 That's why I have to use negation here. 209 00:14:44,410 --> 00:14:51,460 And technically I could call negate method here, but also I can pass this packet to static method not 210 00:14:51,850 --> 00:14:52,600 is it clear. 211 00:14:53,260 --> 00:14:59,260 And when I print the list of users to cancel, I see that I have only users that I wanted to keep in 212 00:14:59,260 --> 00:15:02,080 the list also now you know. 213 00:15:02,210 --> 00:15:08,910 All matters from PAGAD interface and the very last things that I wanted to discuss with you is my predicate 214 00:15:08,930 --> 00:15:09,580 interface. 215 00:15:09,830 --> 00:15:12,590 I believe you already understood how pre-cut works. 216 00:15:12,950 --> 00:15:16,160 The only difference is that test Masset takes. 217 00:15:16,160 --> 00:15:20,810 Two arguments here will use this method when we all stream API. 218 00:15:21,200 --> 00:15:27,410 But the principle of writing and using by predicate is similar to examples that we saw with predicate 219 00:15:28,010 --> 00:15:28,520 beside. 220 00:15:28,520 --> 00:15:37,280 One abstract matter of this interface also has three more default methods Ziya and negate and or methods. 221 00:15:37,730 --> 00:15:41,110 We have just reviewed similar message from pre-record interface. 222 00:15:41,150 --> 00:15:45,770 That's why I think that there is no need to review examples with these methods. 223 00:15:46,280 --> 00:15:48,770 But let me know in case you have any questions. 224 00:15:49,520 --> 00:15:51,590 That's all what I wanted to share with you today. 225 00:15:52,190 --> 00:15:54,700 Now let's recap what we have learned today. 226 00:15:55,370 --> 00:16:01,430 Today we learned with your consumer and by consumer interfaces and real examples, you saw how you can 227 00:16:01,430 --> 00:16:03,590 create LAMDA functions for these types. 228 00:16:04,130 --> 00:16:07,500 After that, we learned precut interface nuance. 229 00:16:07,520 --> 00:16:12,320 All mascot's from pre-record interface and had practice exercises with this type. 230 00:16:12,770 --> 00:16:16,580 And at the end of the lesson we talked about biproduct interface. 231 00:16:17,210 --> 00:16:18,610 That's all for this lesson. 232 00:16:19,160 --> 00:16:20,510 Thanks a lot for your attention. 233 00:16:20,810 --> 00:16:22,400 See you in the next lesson.