Hello, adjustments in this lesson, we're going to learn interfaces from a dual function package,
Ziya function and by function in this lesson, we are going to have a lot of practice to help you understand.
These types better will use map implementation for all examples that will give your understanding how
you can apply your knowledge in real life cases.
And while learning different types will create a lot of lambda functions and MassArt references, we're
going to learn default methods from functional interface.
You will learn what is the difference between and then and compose masses will discuss it identity message
from the function interface and I will explain how it works and what benefit we can get when we use
it at the end of the lesson and will show you how you can create.
Compare that with the help of function that start.
And to learn this topic, it is better to learn it from the source code and then real examples.
We'll start our lesson from the function time function interface is a function interface and declare
one abasic method that takes some value as an argument and can return something.
There are tensions.
That function interface is parametrized by two types.
In case you don't remember what generics are, please look at generic lesson to refresh your knowledge.
Two types and indeed here in parameterization to put a stress on the fact that type of the argument
may be different and type of return to value.
The function is usually applied where you need to produce some result of different type on the basis
of the argument.
Let me show you a function example.
I created separate file for all examples that we are going to review today.
I will leave links to this file in less than attachments.
And as I said, an agenda of the lesson will use classes from Java Collections Framework to show you
how you can apply the knowledge of functional interfaces.
I created an object of hash map type.
Our hash map is parametrized by integer type four keys and string type four values.
I fill out the map with the values for the sake of our demo.
In this line of code, I want to print the value that is associated with the key for I believe you can
understand that no will be printed to console because I didn't put any value for key for now on my map.
I want to go computer absent MassArt.
Let's look at the source code of this matter.
To understand what it is for this method should compute the value for the key in case the key is not
already associated with the value is mapped to now.
And this method will compute the value using the function that we pass to this method.
You can see and Masset arguments that this method takes key as a first argument and also it requires
a function type argument.
And here you can see apply Massata Scholte on our function.
The function that will be given to this method is parametrized by any type that is compatible with the
type of our key and any type that is compatible with the type of the value in this map.
And knowing how a function works, we already can understand that key will be passed to our function
and will use it should be associated with that key will be mapped to it.
Now, let's get back to our example.
I created all examples here before the lesson to save the time during the video recording.
Let me explain to you what is written here.
I call Computer Fab's and Massett and pass the key.
And the second argument is a function from the syntax standpoint.
You can see that in case we have only one argument we can skip parentheses here is a local variable
key and I have to return string tie because the return value should be compatible with the value type
of our map of the sake of the Dharma.
Let me return string representation of four.
Does it make sense?
So now the execution, this lambda function will be called inside compute if absent MASP.
Let me show your source code one more time.
Here apply method of our function will be called and key will be passed.
It will return string value that is not now and after that it will be inserted into the map.
Is it clear?
Let me run the program and print to console the value that is associated with the key for.
And here you can see that before we call compute the absolute mass.
There was no value associated with this key.
And after we call the method, we can see the values that is associated with our key hope.
This example brings you understanding how we can create lambda functions for function interface.
You so housemate's computer.
Perhaps MassArt is written.
It uses function interface as an abstraction and we can pass an implementation of the interface that
we want.
Now I want you to understand that Lambda functions and MassArt references are interchangeable by saying
this.
And that instead of LAMDA function here, we can use MassArt reference.
Let me show you how here in this demo class, I declare a static method that takes some integer as an
argument and return string.
It looks like this method meets the requirements of function, interface and work with Tibs that are
compatible with the current map key and value types.
If compute absent Masset need some functions that can take integer and return string, Azana will tell
it to use convert into string method.
And this line you can see that I used Masset reference to the static MASP.
Does it make sense?
Hope you understood how MassArt references work.
Let's move on.
The next thing I want to talk with you about is by function.
Let me open the source code of my function interface.
You can see that this is also functional interface.
That is parametrized by three types.
It has one abstract method that takes two arguments and returns result.
Now you can understand the naming convention individual function package depends on Ariki with a different
prefix to interface name.
Just to refresh your knowledge, I want to remind you what everybody is pretty is a term that is used
in computer science to specify a number of arguments taken by function.
Regular function takes one argument.
My function takes two arguments.
This prefix is widely used in Java dual function package.
Now you will understand what it means.
You can see the two arguments here and return type.
All of them might be of a different type.
Let me show you the bi functional practice.
Let's return to our demo file now.
I want to compute new value on the basis of existing key and existing value.
I call compute of present Masab Unmap object.
I suggest investigating the source code and documentation for this matter, as we can see here in documentation
in case value for the specified key is present.
And now this method will compute a new map.
And given the key and its current mapped value from the massive declaration, we can understand that
we have to pass some key to this method and by functions at this parametrized by time that is compatible
with the key and types that are compatible with the value based on the BI function interface.
We can understand that the first and the second time in Diament operator are types of the arguments
and the third type from Diamond Operator is a type of return value.
You can also understand the same after investigation of the source code of compute of present MassArt.
Now, let's write our own lambda expression in our demo class, take into account my function requires
two Masset arguments.
I declare two local variables here and we have to return string that will be used as a map and value
two key three just for the sake of the.
Let's create custom string values that consist of the key itself and previous value associated with
the key separated by column.
And you can see in concert that the value associated with the key three is updated.
I believe you already understood how to use massive reference in this situation.
But just in case, I also wrote an example here to reach complete understanding.
Here is a method that is compatible with the expected return type and type of method parameters.
And here you can see that I use this method as method reference instead by function here.
Now, when you understood what function and function are, I want to draw attention to the methods that
we didn't discuss.
Those are default methods from function and by function interfaces.
The way how they work are similar.
That's why we will review on the example with function interface.
Let me open again the source code of function interface.
Here you can see that we have default methods Campo's and and then methods.
These methods allow you to compose new function from existing ones to understand this.
Let me get back to the demo file to show you an example.
Consider the next case.
We have product type products, price and idee.
That is a unique product identifier.
We also have getters and setters declared here.
Besides that, I overwrite the string MassArt.
This will help us during the debugging when we will print product to console for the sake of the demo
and declared all types in one file do not switch between tops in eclipse.
That's why you can find this glass at the bottom of our demo file.
Now imagine that we have a map object that should map product against discount amount so that I could
get discount by product.
That's why my map is parametrized with product type four keys and double type four values.
This example is just to help you to understand the case.
Imagine that I have a list of products in real life.
I can fetch this data from the database or pass from some file.
Each product has its IP and price.
Now I have to fill out my map.
How I would do that.
I create for each loop to iterate over all products and on each iteration I compose two functions.
The first function just extract the price.
The second function calculates discount.
Based on this price, I use constant value for that.
This means five percent discount and I want to get the amount of money for my discount.
That's why you see multiplication here.
After that, I combine these two functions.
I combine them in the way the first function will be get the price function and the second function
will be a discount function.
Probably using that is a lot of good.
And you could calculate discount in one function here, but focus on the fact that you can combine two
functions.
This example is about that you can create new function on the basis of already existing functions.
You can make them as you wish.
Next time you don't get price combined with discount function, but you need to get price and for example,
get that function.
Can you understand now the benefit of function composition?
This gives you flexibility in combination of functions and after that I call comparative absence method
past the key to this method that would be product and functions.
It calculates the discount.
Let me print the map that we received to consult and here you can see that each product is mapped to
the discount.
Is it clear now when you know how and then method works?
Let me show you how composed Method works.
The only difference between these two methods is the order in which functions are combined.
In case I use function, then call and that method and pass another function.
This function will be executed after the first one and we'll take the result of the first function as
an input argument.
Does it make sense?
In case I use compose method, then I should specify function that should go first.
So technically these two functions are the same, but the orders that are used in syntax is different.
In my second function, I use that discount function, the call compose method and parsing its price
function to it.
That means get price function will be executed first and after that the discount function will be executed
with a result of get price function.
I hope I didn't get.
The future now, you know, the difference between Enza and Campo's default mascot's in function interface.
Let me open my function interface now.
Here you can see only one default method.
And then knowing how to create lambda function for my function interface and knowing how.
And then Masset works in function interface.
I believe you would be able to apply this method during the development when it will be needed.
There is also one more default method in function interface that we didn't talk through.
I'm talking about identity must pay attention to the source code of this message.
This function always returns its inputs.
Argument here is simple lambda expression for that.
But should we use this method instead of similar lambda function?
Yes, we should, because right in lambda expression will create an instance of function type after
each occurrence of lambda function, whereas column identity method you would get the always the same
instance of the function.
So using function identity instead of this lambda function might save you some memory, but that shouldn't
drive your decision.
If you really think that lambda function is more readable than function identity, I would love to show
you the example of this method and tell you where and when you can apply this method.
But we need knowledge of Stream API to understand the example.
Basically, we use this method only when we are forced to pass function, but we don't need to modify
inputs.
Argument you will face with such cases while working with stream API.
Don't worry.
In the lessons about Stream API, we'll talk about this method.
And the last but not least examples that I wanted to share with you today is how to create comparator
with helper function.
Let me open compared the interface.
And if you remember, the computer interface has a lot of default methods.
Some of them use function interface.
I'm going to show you one example that will give you understand how you can use the rest of the methods
that they function as an argument.
Here we have really cool method that most likely you will use.
It is called comparing it grades compare to on the basis of the function that extracts the comparable
key that should be used to sort by to help you understand uses.
Let me show you an example how this works.
Today we created a list of products and if you remember that, imagine that I want to short list by
its price.
That's why I call sort Masset on my list.
I have to pass Comparator here.
I don't need to create a concrete class of comparator type.
I also don't need to implement comparable interface in my product type.
I can just pass the function that can extract the field that should be used during the sorting.
And I already have this function.
Let me pass the message reference together in my product class.
This guy knows how to retrieve price from each product taken into account.
I have a list of products here and I don't have it in the file for product.
I can use a class name to make a reference to Macit Compiler is smart enough to understand that this
method should be called on.
Each object from this list to extract K double is comparable type.
That's why certain will be performed without any issues.
And here is an analogy written with the help of Lambda function.
These two lines are similar in console.
We can see that when I print at least all elements assaulted by price.
So now you know how to create a comparator by using function.
That's all examples that I wanted to discuss with you in this lesson.
Let's recap what we have learned today.
Today we learned what function and function interfaces are.
We learned what methods they have, and we know how to create lambda functions and Masset references
for these interfaces.
We learn how and then and compose methods work.
Now, you know the difference between these two methods.
I believe after this lesson, you also know how identity method works in function interface and also
today had a lot of practice.
And in real life example, we learned how we can use function and by function during the work with MAP
implementations.
At the end of the lesson, you saw how to create Comparator with the help of a function.
That's all what I have for you for today.
Thanks a lot for your attention.
Have a great day and see you in the next lesson.