Hello.
Yes, students in this class and we are going to learn what lambda expressions and message references
are today.
We are going to have really a lot of practice after this lesson.
You will be able to write lambda functions like a pro.
I will explain to you what functional interfaces are and we will learn the difference between regular
and functional interfaces by the end of the class and you will know how to create your own functional
interfaces.
We are going to learn what the function is and what Massett reference is.
I prepared special examples that will help you to grasp the knowledge faster.
I'm going to show you how you can write lambda functions and how you can use math references.
Let's start.
And today, the best way to learn the topic is start from examples.
That's why I suggest switching to the eclipse and start our lesson.
To understand how to use Lambda functions and Macit references, we have to understand first what functional
interface is.
As we briefly discussed in previous lesson, functional interface is an interface that has a single
object method declared in it.
Take a look at this example, this interface is called Distance Calculator.
It is declared to have Iraqi of types that can calculate distance of delivery of customer's order in
miles.
I can calculate the shortest path, the most optimal road route for interstate delivery, distance from
the closest store or from the specific store and lots more.
All this might be implemented in calculate distance method in other types that will implement the Easton's
calculator.
This method also takes two objects of city type.
The store inside coordinates.
We have city time declared here.
This is a type created just for our example.
The state of the city can be described with the help of the coordinates, longitude and latitude, and
we have getters and setters here.
Now you have the context of real life business case.
I believe that understanding of real life example will help you to grasp new knowledge faster.
That is great.
You can see new annotations here, functional interface.
What is it for?
First of all, this is a great sign to notify clients of your code that originally regionalisms interface
is created to be a target type for lambda functions and Macit references.
And the second reason to use the sanitation is that in case somebody would try to add additional abasic
Macit, there will be a compilation error.
Let me comment this line of code that contains declaration of additional Massett.
And here you can see we have a compilation error now.
That's why functional interface annotation helps us to prevent cases when someone wants to add additional
behavior to the type that we already used in other parts of the program as a tag for lambda function.
Hope it is clear what function interface is.
Let me comment this MassArt again to proceed.
Pay attention to the fact that functional interface can contain as much default methods as possible
and as much static methods as possible.
This does not violate the rule for functional interface.
That said, regarding functional interface, that was easy and great.
Let me congratulate you.
Now you know how to create your own custom functional interface.
And now when you know what functional interface is, let's learn what lambda expressions are.
Let me open the next examples that I have prepared before this lesson.
I have a few more classes grouped here in one file, just for the sake of example, to not switch tabs
between different files.
Imagine that we have class with a name or the management, and this class requires an instance of distance
calculator type to perform all operations related to the management we can pass in terms of distance
calculator type, either with the help of constructor or with the help of the setup here.
That means you need to create an object of type distance calculator in your program, but you know that
you can't create objects on the basis of interface, right?
That's why you declare concrete class, let's say the full distance calculator and implement distance
calculator interface here.
Now when we create object of type order management, we great object of type distance calculator.
But what to do in case we need to change this since calculator to take into account specifics of inter-state
delivery.
In this case, we have to create another concrete class and we need this just override, calculate distance
method.
Only once is it worth it.
We've seen that this doesn't work of our efforts and we decide to declare anonymous class and create
an instance of anonymous class.
This time we take into account logic for interstate distance calculation.
OK, but can you see how much lines of code it takes?
It is super weird that we have to declare a concrete class just to give implementation of one single
method.
We can create thousands of concrete classes to implement dozens of functions.
But do we need to overwhelm our application with additional classes if we just need one function in
one single place?
No, that's where lambda function is going to help us.
But what is lambda function?
It is also called anonymous function.
It doesn't have any name.
Usually anonymous functions are passed to higher order functions.
Also, in case the function is only used once a limited number of times, it will be easier to Islam
the expression rather than the clear and concrete class in need to create an object of time distance
calculator.
We have to just implement one method, take a look here and declare a new variable of distance calculator
type to describe new logic for calculate distance method.
And here you can see the land expression.
Here it is lambda function, anonymous function.
We can call this in different ways by using different names.
Let me explain in the syntax here.
You can see.
Parentheses that we usually use for MassArt invocation, Autobus arguments, you can see that I Passey
Q1 and Q2 arguments where I this I didn't create any objects.
This is just local MASAD variables similar to the ones we use in massive declaration.
Using these variables I can get access to objects at my function should work with inside the body.
I will show you that in a second.
Why I don't write types of these local variables because compiler knows that I want to give implementation
of the method from distance calculator type and the first argument.
And this method has typesetter the same as the second argument.
That's why Compiler knows that these local variables should be of typesetter.
But in case I will add types here, there is no harm in this, just like this.
In case if Jason's calculator wouldn't declare any Masset parameters and calculate distance measured
in that case, we would skip Masad arguments here and would leave just blank parentheses like this.
Here is LAMDA and here goes returned value.
Here is some damage calculation.
For example, I perform subtraction of longitude of 31 and 32.
That doesn't give us a true distance between cities, but makes you understand that you can perform
any calculations here.
And the result of this calculation is returns from the MassArt worse given return keyword here, because
Compellent knows that at one point of implementation of the absolute masset of distance calculate the
time and compiler knows that this method should return value.
Technically creation of object of anonymous glass.
And this is the same because calculate distance implementation is the same as here.
The next point you have to know about our Syntex in case our anonymous function consists of more than
one line, then we need to use Brace's.
And here you can see an example.
Also, in this case, we have to right return keywords.
And if you see that in this case, visually, this LAMDA function is closer to the method we have here
in anonymous class where missing only a return type value method, name type of local variables and
access modifier.
So in case I write here, public double calculate distance and in case I would add types to local variables,
does it make more sense like this?
But for lambda expression, we don't need to specify access modifier, it is already accessed here,
let me remove it also.
We don't have to specify return that because our Java compiler can understand that we are going to implement
Macit from a distance, calculate the functional interface and the method inside this interface has
double return type.
Can you understand now why it is important that functional interface has only one abstract method?
Because in case functional interface has more than one method, then it is not clear what message I
try to implement with the help of anonymous function.
Is that clear?
So I can remove, recontact and also I can remove massive name because it is clear what method I want
to implement and I don't need any other identifier to access this function.
And I believe you already understood why I can remove all types of my local variables, because compiler
knows that these variables will be of type according to the interface here you can see that I can call
calculate distance method on my reference here and I believe you already understood that these lines
of code will be executed.
Let me prove this.
I run the program and here you can see console output from the lambda function.
And when I need implementation of distance calculator interface and I need it only one time only it
is easier for me to create lambda function rather than declare a concrete class and create an instance
of a concrete class.
For example, here in the set method, I can pass any function I need and that will be enough for order
management because all the management is interested not in the fact of get an object of type discount
calculator.
Order management is interested in getting behavior to calculate distance and that gives a specific definition
of behavior with the help of lambda function.
Usually it is not instantly clear for my students where we will get arguments for the lambda function.
I want you to understand that lambda function.
It is not a method invocation with arguments.
It is just the definition of the method with parameters similar to this thing we do when we implement
any interface in concrete class.
So the city objects will be passed here to this function, wherever it will be needed.
According to the logic described in the order management.
Sometimes we only know that some type like order management just needs logic to calculate distance between
two cities.
And sometimes we really don't care where order management will get those cities.
Probably they will be passed to other methods of order management or will be present in all the details.
But we let all the management object decide when to call our function and we let all the management
bother about what just to pass there.
Does it make sense?
Hope you understood what lambda function is.
Now, let me explain to you what a reference is.
Imagine that you already have mass.
That's exactly the same what you want.
Probably it is from some external library or probably it is just from another class.
Then why do we need to declare a concrete class to override MassArt or create lambda function?
Why don't we just tell the order management object to use the mass that already exists?
In all cases, when all the management needs to calculate distance, we can do so.
And for this we have to use MassArt references.
Let me show you the next example.
Here is the example of some third party class.
Imagine that it is called Google Dist. calculator and it has Masset for distance calculation.
It is called get distance between cities.
You can see that we have static and non static MassArt.
That is, for the sake of example, to cover a few cases.
Please pay attention to the fact that type of return value and type of method parameters is the same
as we have in distance calculator interface.
This is important.
And now when I need to set new distance calculator into my or the management object, I just pass the
reference to this method.
Let me explain this.
This index here here is the name of our class that contains the message that we need, followed by double
column.
This is a syntax of reference I'm referencing to the distance between a static method in Google distance
calculator class.
Does it make sense?
And again, this is not in location.
That's why I don't pass any arguments here, or the management just needs something that can take two
cities and will return double value.
That's all what order management object expects from distance calculator type and this method from Google
Distance Calculator do the exact things that order management wants.
That's why there is no compilation error here, pay attention that I use class name to make a reference
to static method in case I want to make a reference to non-cyclical method.
I have to create object.
And here you can see that I created object.
Now I can use variable of my object to make a reference to the non static method.
Does it make sense?
Hope now it is clear how Massata references work.
They learned a lot today.
Let's recap what we have learned today, the day we learned what functional interface is and we even
created a custom functional interface.
Now you know what the function is and how to write your own lambda expression.
After that, we learned what math reference is.
On the real example, we learned how to apply your knowledge.
That's it for this lesson.
Thank you for your attention.
See you in the next lesson.