1 00:00:06,290 --> 00:00:11,450 Hello, adjustments in this lesson, we're going to learn interfaces from a dual function package, 2 00:00:11,780 --> 00:00:18,920 Ziya function and by function in this lesson, we are going to have a lot of practice to help you understand. 3 00:00:18,920 --> 00:00:25,700 These types better will use map implementation for all examples that will give your understanding how 4 00:00:25,700 --> 00:00:28,100 you can apply your knowledge in real life cases. 5 00:00:28,610 --> 00:00:34,700 And while learning different types will create a lot of lambda functions and MassArt references, we're 6 00:00:34,700 --> 00:00:37,490 going to learn default methods from functional interface. 7 00:00:37,640 --> 00:00:43,820 You will learn what is the difference between and then and compose masses will discuss it identity message 8 00:00:43,820 --> 00:00:49,310 from the function interface and I will explain how it works and what benefit we can get when we use 9 00:00:49,310 --> 00:00:52,390 it at the end of the lesson and will show you how you can create. 10 00:00:52,400 --> 00:00:55,550 Compare that with the help of function that start. 11 00:00:56,210 --> 00:01:00,830 And to learn this topic, it is better to learn it from the source code and then real examples. 12 00:01:01,400 --> 00:01:07,520 We'll start our lesson from the function time function interface is a function interface and declare 13 00:01:07,520 --> 00:01:12,200 one abasic method that takes some value as an argument and can return something. 14 00:01:12,740 --> 00:01:13,550 There are tensions. 15 00:01:13,550 --> 00:01:16,670 That function interface is parametrized by two types. 16 00:01:17,120 --> 00:01:22,220 In case you don't remember what generics are, please look at generic lesson to refresh your knowledge. 17 00:01:22,850 --> 00:01:28,820 Two types and indeed here in parameterization to put a stress on the fact that type of the argument 18 00:01:28,820 --> 00:01:31,390 may be different and type of return to value. 19 00:01:31,910 --> 00:01:37,250 The function is usually applied where you need to produce some result of different type on the basis 20 00:01:37,250 --> 00:01:38,340 of the argument. 21 00:01:38,690 --> 00:01:40,550 Let me show you a function example. 22 00:01:40,910 --> 00:01:44,660 I created separate file for all examples that we are going to review today. 23 00:01:45,140 --> 00:01:48,140 I will leave links to this file in less than attachments. 24 00:01:48,290 --> 00:01:53,420 And as I said, an agenda of the lesson will use classes from Java Collections Framework to show you 25 00:01:53,420 --> 00:01:56,500 how you can apply the knowledge of functional interfaces. 26 00:01:57,170 --> 00:01:59,510 I created an object of hash map type. 27 00:01:59,840 --> 00:02:05,120 Our hash map is parametrized by integer type four keys and string type four values. 28 00:02:05,540 --> 00:02:08,810 I fill out the map with the values for the sake of our demo. 29 00:02:09,260 --> 00:02:15,320 In this line of code, I want to print the value that is associated with the key for I believe you can 30 00:02:15,320 --> 00:02:22,820 understand that no will be printed to console because I didn't put any value for key for now on my map. 31 00:02:22,820 --> 00:02:25,100 I want to go computer absent MassArt. 32 00:02:25,580 --> 00:02:27,530 Let's look at the source code of this matter. 33 00:02:27,540 --> 00:02:33,890 To understand what it is for this method should compute the value for the key in case the key is not 34 00:02:33,890 --> 00:02:37,040 already associated with the value is mapped to now. 35 00:02:37,400 --> 00:02:42,050 And this method will compute the value using the function that we pass to this method. 36 00:02:42,590 --> 00:02:49,430 You can see and Masset arguments that this method takes key as a first argument and also it requires 37 00:02:49,430 --> 00:02:50,960 a function type argument. 38 00:02:51,710 --> 00:02:55,520 And here you can see apply Massata Scholte on our function. 39 00:02:56,000 --> 00:03:02,060 The function that will be given to this method is parametrized by any type that is compatible with the 40 00:03:02,060 --> 00:03:07,640 type of our key and any type that is compatible with the type of the value in this map. 41 00:03:08,210 --> 00:03:14,150 And knowing how a function works, we already can understand that key will be passed to our function 42 00:03:14,660 --> 00:03:18,590 and will use it should be associated with that key will be mapped to it. 43 00:03:19,340 --> 00:03:21,470 Now, let's get back to our example. 44 00:03:22,010 --> 00:03:26,600 I created all examples here before the lesson to save the time during the video recording. 45 00:03:26,870 --> 00:03:28,970 Let me explain to you what is written here. 46 00:03:29,450 --> 00:03:32,570 I call Computer Fab's and Massett and pass the key. 47 00:03:32,930 --> 00:03:36,920 And the second argument is a function from the syntax standpoint. 48 00:03:36,920 --> 00:03:43,850 You can see that in case we have only one argument we can skip parentheses here is a local variable 49 00:03:43,850 --> 00:03:50,270 key and I have to return string tie because the return value should be compatible with the value type 50 00:03:50,270 --> 00:03:52,870 of our map of the sake of the Dharma. 51 00:03:52,910 --> 00:03:55,520 Let me return string representation of four. 52 00:03:56,150 --> 00:03:57,200 Does it make sense? 53 00:03:57,920 --> 00:04:04,160 So now the execution, this lambda function will be called inside compute if absent MASP. 54 00:04:04,670 --> 00:04:07,010 Let me show your source code one more time. 55 00:04:07,640 --> 00:04:12,980 Here apply method of our function will be called and key will be passed. 56 00:04:13,070 --> 00:04:19,490 It will return string value that is not now and after that it will be inserted into the map. 57 00:04:19,790 --> 00:04:20,570 Is it clear? 58 00:04:21,470 --> 00:04:26,420 Let me run the program and print to console the value that is associated with the key for. 59 00:04:26,990 --> 00:04:30,450 And here you can see that before we call compute the absolute mass. 60 00:04:30,680 --> 00:04:33,410 There was no value associated with this key. 61 00:04:33,980 --> 00:04:39,440 And after we call the method, we can see the values that is associated with our key hope. 62 00:04:39,440 --> 00:04:44,450 This example brings you understanding how we can create lambda functions for function interface. 63 00:04:45,020 --> 00:04:46,820 You so housemate's computer. 64 00:04:46,830 --> 00:04:48,260 Perhaps MassArt is written. 65 00:04:48,680 --> 00:04:54,590 It uses function interface as an abstraction and we can pass an implementation of the interface that 66 00:04:54,590 --> 00:04:55,160 we want. 67 00:04:55,820 --> 00:05:02,240 Now I want you to understand that Lambda functions and MassArt references are interchangeable by saying 68 00:05:02,240 --> 00:05:02,600 this. 69 00:05:02,830 --> 00:05:06,620 And that instead of LAMDA function here, we can use MassArt reference. 70 00:05:07,010 --> 00:05:13,310 Let me show you how here in this demo class, I declare a static method that takes some integer as an 71 00:05:13,310 --> 00:05:15,080 argument and return string. 72 00:05:15,620 --> 00:05:20,900 It looks like this method meets the requirements of function, interface and work with Tibs that are 73 00:05:20,900 --> 00:05:23,720 compatible with the current map key and value types. 74 00:05:24,290 --> 00:05:30,590 If compute absent Masset need some functions that can take integer and return string, Azana will tell 75 00:05:30,590 --> 00:05:32,960 it to use convert into string method. 76 00:05:33,440 --> 00:05:37,930 And this line you can see that I used Masset reference to the static MASP. 77 00:05:38,390 --> 00:05:39,210 Does it make sense? 78 00:05:39,840 --> 00:05:42,280 Hope you understood how MassArt references work. 79 00:05:42,860 --> 00:05:43,600 Let's move on. 80 00:05:44,480 --> 00:05:47,660 The next thing I want to talk with you about is by function. 81 00:05:48,050 --> 00:05:50,930 Let me open the source code of my function interface. 82 00:05:51,260 --> 00:05:54,080 You can see that this is also functional interface. 83 00:05:54,080 --> 00:05:56,180 That is parametrized by three types. 84 00:05:56,600 --> 00:06:00,920 It has one abstract method that takes two arguments and returns result. 85 00:06:01,410 --> 00:06:08,570 Now you can understand the naming convention individual function package depends on Ariki with a different 86 00:06:08,570 --> 00:06:10,130 prefix to interface name. 87 00:06:10,790 --> 00:06:16,940 Just to refresh your knowledge, I want to remind you what everybody is pretty is a term that is used 88 00:06:16,940 --> 00:06:21,290 in computer science to specify a number of arguments taken by function. 89 00:06:21,830 --> 00:06:24,110 Regular function takes one argument. 90 00:06:24,590 --> 00:06:26,930 My function takes two arguments. 91 00:06:27,560 --> 00:06:31,000 This prefix is widely used in Java dual function package. 92 00:06:31,160 --> 00:06:33,380 Now you will understand what it means. 93 00:06:33,980 --> 00:06:36,980 You can see the two arguments here and return type. 94 00:06:36,980 --> 00:06:39,020 All of them might be of a different type. 95 00:06:39,500 --> 00:06:42,080 Let me show you the bi functional practice. 96 00:06:42,530 --> 00:06:45,140 Let's return to our demo file now. 97 00:06:45,140 --> 00:06:49,760 I want to compute new value on the basis of existing key and existing value. 98 00:06:50,270 --> 00:06:52,940 I call compute of present Masab Unmap object. 99 00:06:53,300 --> 00:07:00,170 I suggest investigating the source code and documentation for this matter, as we can see here in documentation 100 00:07:00,320 --> 00:07:03,230 in case value for the specified key is present. 101 00:07:03,230 --> 00:07:06,470 And now this method will compute a new map. 102 00:07:06,470 --> 00:07:12,500 And given the key and its current mapped value from the massive declaration, we can understand that 103 00:07:12,500 --> 00:07:18,650 we have to pass some key to this method and by functions at this parametrized by time that is compatible 104 00:07:18,650 --> 00:07:24,320 with the key and types that are compatible with the value based on the BI function interface. 105 00:07:24,320 --> 00:07:30,530 We can understand that the first and the second time in Diament operator are types of the arguments 106 00:07:30,950 --> 00:07:34,810 and the third type from Diamond Operator is a type of return value. 107 00:07:35,330 --> 00:07:40,400 You can also understand the same after investigation of the source code of compute of present MassArt. 108 00:07:42,840 --> 00:07:49,560 Now, let's write our own lambda expression in our demo class, take into account my function requires 109 00:07:49,560 --> 00:07:50,820 two Masset arguments. 110 00:07:50,820 --> 00:07:56,800 I declare two local variables here and we have to return string that will be used as a map and value 111 00:07:56,940 --> 00:07:59,430 two key three just for the sake of the. 112 00:08:00,030 --> 00:08:05,740 Let's create custom string values that consist of the key itself and previous value associated with 113 00:08:05,740 --> 00:08:07,350 the key separated by column. 114 00:08:07,770 --> 00:08:12,600 And you can see in concert that the value associated with the key three is updated. 115 00:08:12,960 --> 00:08:17,130 I believe you already understood how to use massive reference in this situation. 116 00:08:17,370 --> 00:08:22,140 But just in case, I also wrote an example here to reach complete understanding. 117 00:08:22,680 --> 00:08:28,470 Here is a method that is compatible with the expected return type and type of method parameters. 118 00:08:29,040 --> 00:08:33,760 And here you can see that I use this method as method reference instead by function here. 119 00:08:34,260 --> 00:08:40,020 Now, when you understood what function and function are, I want to draw attention to the methods that 120 00:08:40,020 --> 00:08:40,980 we didn't discuss. 121 00:08:41,460 --> 00:08:45,210 Those are default methods from function and by function interfaces. 122 00:08:45,760 --> 00:08:47,640 The way how they work are similar. 123 00:08:47,650 --> 00:08:51,090 That's why we will review on the example with function interface. 124 00:08:51,390 --> 00:08:54,240 Let me open again the source code of function interface. 125 00:08:54,810 --> 00:08:59,310 Here you can see that we have default methods Campo's and and then methods. 126 00:08:59,820 --> 00:09:05,070 These methods allow you to compose new function from existing ones to understand this. 127 00:09:05,100 --> 00:09:07,890 Let me get back to the demo file to show you an example. 128 00:09:08,460 --> 00:09:09,720 Consider the next case. 129 00:09:10,020 --> 00:09:13,550 We have product type products, price and idee. 130 00:09:14,010 --> 00:09:15,920 That is a unique product identifier. 131 00:09:16,320 --> 00:09:18,800 We also have getters and setters declared here. 132 00:09:19,290 --> 00:09:21,990 Besides that, I overwrite the string MassArt. 133 00:09:22,530 --> 00:09:27,840 This will help us during the debugging when we will print product to console for the sake of the demo 134 00:09:27,960 --> 00:09:32,250 and declared all types in one file do not switch between tops in eclipse. 135 00:09:32,850 --> 00:09:36,700 That's why you can find this glass at the bottom of our demo file. 136 00:09:37,380 --> 00:09:43,860 Now imagine that we have a map object that should map product against discount amount so that I could 137 00:09:43,860 --> 00:09:45,300 get discount by product. 138 00:09:45,790 --> 00:09:51,300 That's why my map is parametrized with product type four keys and double type four values. 139 00:09:51,570 --> 00:09:54,810 This example is just to help you to understand the case. 140 00:09:55,380 --> 00:09:58,890 Imagine that I have a list of products in real life. 141 00:09:58,890 --> 00:10:02,780 I can fetch this data from the database or pass from some file. 142 00:10:03,210 --> 00:10:05,950 Each product has its IP and price. 143 00:10:06,870 --> 00:10:09,120 Now I have to fill out my map. 144 00:10:09,450 --> 00:10:10,920 How I would do that. 145 00:10:11,370 --> 00:10:17,880 I create for each loop to iterate over all products and on each iteration I compose two functions. 146 00:10:18,270 --> 00:10:21,020 The first function just extract the price. 147 00:10:21,360 --> 00:10:23,970 The second function calculates discount. 148 00:10:23,970 --> 00:10:27,510 Based on this price, I use constant value for that. 149 00:10:27,990 --> 00:10:33,380 This means five percent discount and I want to get the amount of money for my discount. 150 00:10:33,750 --> 00:10:36,160 That's why you see multiplication here. 151 00:10:36,690 --> 00:10:39,630 After that, I combine these two functions. 152 00:10:39,900 --> 00:10:45,690 I combine them in the way the first function will be get the price function and the second function 153 00:10:45,690 --> 00:10:47,520 will be a discount function. 154 00:10:48,090 --> 00:10:50,850 Probably using that is a lot of good. 155 00:10:50,850 --> 00:10:57,630 And you could calculate discount in one function here, but focus on the fact that you can combine two 156 00:10:57,630 --> 00:10:58,300 functions. 157 00:10:58,860 --> 00:11:05,490 This example is about that you can create new function on the basis of already existing functions. 158 00:11:05,850 --> 00:11:07,750 You can make them as you wish. 159 00:11:08,340 --> 00:11:15,660 Next time you don't get price combined with discount function, but you need to get price and for example, 160 00:11:15,660 --> 00:11:17,090 get that function. 161 00:11:17,640 --> 00:11:20,930 Can you understand now the benefit of function composition? 162 00:11:21,600 --> 00:11:28,140 This gives you flexibility in combination of functions and after that I call comparative absence method 163 00:11:28,470 --> 00:11:32,220 past the key to this method that would be product and functions. 164 00:11:32,220 --> 00:11:33,690 It calculates the discount. 165 00:11:34,320 --> 00:11:40,170 Let me print the map that we received to consult and here you can see that each product is mapped to 166 00:11:40,170 --> 00:11:40,850 the discount. 167 00:11:41,250 --> 00:11:45,460 Is it clear now when you know how and then method works? 168 00:11:45,480 --> 00:11:47,820 Let me show you how composed Method works. 169 00:11:48,230 --> 00:11:53,400 The only difference between these two methods is the order in which functions are combined. 170 00:11:53,790 --> 00:11:58,880 In case I use function, then call and that method and pass another function. 171 00:11:59,250 --> 00:12:05,700 This function will be executed after the first one and we'll take the result of the first function as 172 00:12:05,700 --> 00:12:06,870 an input argument. 173 00:12:07,260 --> 00:12:08,310 Does it make sense? 174 00:12:08,940 --> 00:12:14,430 In case I use compose method, then I should specify function that should go first. 175 00:12:14,940 --> 00:12:22,290 So technically these two functions are the same, but the orders that are used in syntax is different. 176 00:12:22,740 --> 00:12:29,090 In my second function, I use that discount function, the call compose method and parsing its price 177 00:12:29,100 --> 00:12:30,090 function to it. 178 00:12:30,750 --> 00:12:38,520 That means get price function will be executed first and after that the discount function will be executed 179 00:12:38,540 --> 00:12:40,610 with a result of get price function. 180 00:12:40,610 --> 00:12:42,140 I hope I didn't get. 181 00:12:42,220 --> 00:12:48,470 The future now, you know, the difference between Enza and Campo's default mascot's in function interface. 182 00:12:49,120 --> 00:12:51,550 Let me open my function interface now. 183 00:12:52,210 --> 00:12:54,700 Here you can see only one default method. 184 00:12:54,850 --> 00:13:00,310 And then knowing how to create lambda function for my function interface and knowing how. 185 00:13:00,310 --> 00:13:02,740 And then Masset works in function interface. 186 00:13:03,010 --> 00:13:07,540 I believe you would be able to apply this method during the development when it will be needed. 187 00:13:08,170 --> 00:13:12,620 There is also one more default method in function interface that we didn't talk through. 188 00:13:13,000 --> 00:13:17,810 I'm talking about identity must pay attention to the source code of this message. 189 00:13:18,340 --> 00:13:20,950 This function always returns its inputs. 190 00:13:20,950 --> 00:13:24,430 Argument here is simple lambda expression for that. 191 00:13:24,970 --> 00:13:28,530 But should we use this method instead of similar lambda function? 192 00:13:29,050 --> 00:13:34,870 Yes, we should, because right in lambda expression will create an instance of function type after 193 00:13:34,870 --> 00:13:41,410 each occurrence of lambda function, whereas column identity method you would get the always the same 194 00:13:41,410 --> 00:13:42,770 instance of the function. 195 00:13:43,390 --> 00:13:49,900 So using function identity instead of this lambda function might save you some memory, but that shouldn't 196 00:13:49,900 --> 00:13:51,010 drive your decision. 197 00:13:51,010 --> 00:13:56,920 If you really think that lambda function is more readable than function identity, I would love to show 198 00:13:56,920 --> 00:14:01,540 you the example of this method and tell you where and when you can apply this method. 199 00:14:01,930 --> 00:14:05,320 But we need knowledge of Stream API to understand the example. 200 00:14:05,870 --> 00:14:12,160 Basically, we use this method only when we are forced to pass function, but we don't need to modify 201 00:14:12,160 --> 00:14:12,550 inputs. 202 00:14:12,550 --> 00:14:16,870 Argument you will face with such cases while working with stream API. 203 00:14:17,380 --> 00:14:18,210 Don't worry. 204 00:14:18,220 --> 00:14:21,580 In the lessons about Stream API, we'll talk about this method. 205 00:14:22,300 --> 00:14:27,760 And the last but not least examples that I wanted to share with you today is how to create comparator 206 00:14:27,760 --> 00:14:28,990 with helper function. 207 00:14:29,440 --> 00:14:31,450 Let me open compared the interface. 208 00:14:31,780 --> 00:14:36,340 And if you remember, the computer interface has a lot of default methods. 209 00:14:36,790 --> 00:14:39,160 Some of them use function interface. 210 00:14:39,430 --> 00:14:45,100 I'm going to show you one example that will give you understand how you can use the rest of the methods 211 00:14:45,130 --> 00:14:46,960 that they function as an argument. 212 00:14:47,560 --> 00:14:51,040 Here we have really cool method that most likely you will use. 213 00:14:51,190 --> 00:14:57,430 It is called comparing it grades compare to on the basis of the function that extracts the comparable 214 00:14:57,430 --> 00:15:02,080 key that should be used to sort by to help you understand uses. 215 00:15:02,230 --> 00:15:04,630 Let me show you an example how this works. 216 00:15:05,290 --> 00:15:11,110 Today we created a list of products and if you remember that, imagine that I want to short list by 217 00:15:11,110 --> 00:15:11,950 its price. 218 00:15:12,440 --> 00:15:15,340 That's why I call sort Masset on my list. 219 00:15:15,670 --> 00:15:17,440 I have to pass Comparator here. 220 00:15:18,040 --> 00:15:21,670 I don't need to create a concrete class of comparator type. 221 00:15:22,030 --> 00:15:26,980 I also don't need to implement comparable interface in my product type. 222 00:15:27,490 --> 00:15:32,350 I can just pass the function that can extract the field that should be used during the sorting. 223 00:15:32,800 --> 00:15:34,600 And I already have this function. 224 00:15:35,200 --> 00:15:38,730 Let me pass the message reference together in my product class. 225 00:15:39,130 --> 00:15:44,680 This guy knows how to retrieve price from each product taken into account. 226 00:15:44,680 --> 00:15:48,940 I have a list of products here and I don't have it in the file for product. 227 00:15:49,210 --> 00:15:56,170 I can use a class name to make a reference to Macit Compiler is smart enough to understand that this 228 00:15:56,170 --> 00:15:57,490 method should be called on. 229 00:15:57,490 --> 00:16:02,620 Each object from this list to extract K double is comparable type. 230 00:16:03,100 --> 00:16:06,400 That's why certain will be performed without any issues. 231 00:16:07,120 --> 00:16:10,510 And here is an analogy written with the help of Lambda function. 232 00:16:10,900 --> 00:16:13,900 These two lines are similar in console. 233 00:16:13,900 --> 00:16:18,100 We can see that when I print at least all elements assaulted by price. 234 00:16:18,460 --> 00:16:22,270 So now you know how to create a comparator by using function. 235 00:16:22,780 --> 00:16:26,260 That's all examples that I wanted to discuss with you in this lesson. 236 00:16:26,650 --> 00:16:28,840 Let's recap what we have learned today. 237 00:16:29,500 --> 00:16:32,890 Today we learned what function and function interfaces are. 238 00:16:33,310 --> 00:16:38,860 We learned what methods they have, and we know how to create lambda functions and Masset references 239 00:16:38,860 --> 00:16:40,210 for these interfaces. 240 00:16:40,750 --> 00:16:43,810 We learn how and then and compose methods work. 241 00:16:44,260 --> 00:16:47,110 Now, you know the difference between these two methods. 242 00:16:47,590 --> 00:16:54,160 I believe after this lesson, you also know how identity method works in function interface and also 243 00:16:54,190 --> 00:16:55,930 today had a lot of practice. 244 00:16:55,930 --> 00:17:01,840 And in real life example, we learned how we can use function and by function during the work with MAP 245 00:17:01,840 --> 00:17:02,770 implementations. 246 00:17:03,280 --> 00:17:07,780 At the end of the lesson, you saw how to create Comparator with the help of a function. 247 00:17:08,380 --> 00:17:10,240 That's all what I have for you for today. 248 00:17:10,690 --> 00:17:12,040 Thanks a lot for your attention. 249 00:17:12,290 --> 00:17:15,040 Have a great day and see you in the next lesson.