Hello.
Students, in this lesson, we're going to learn what consumer by consumer product and by product interfaces
are as usual.
We'll start from an investigation of a source code of all interfaces.
We'll review all matters that are defined there.
One real example will show you how you can use these types and how to implement LAMDA functions and
methods, references to implement consumer by consumer product and by product types.
After that, we'll talk about two other types.
We are also going to default and static methods from these interfaces.
We have a lot of things to learn today.
Let's start.
The best way to learn this topic is to investigate the source code and the real examples.
I suggest starting our lesson today from the overview of consumer interface source code.
As you can see, similar to other types declared in Geritol function package consumer is a functional
interface that has one abstract method with a name, except it takes some argument and returns nothing.
So this type exists to perform an operation on the given argument, unlike other functions.
This type of function usually produces side effect.
I'm talking about that.
The reason to use this type is to modify the state of the argument and create a function for that.
This interface also has one default method.
This is and then Masset, in the previous lesson we learned how we can use and then MassArt to compose
new function on the basis of two other functions.
It works the same way with consumer.
That's why it will not pay a lot of attention to this method.
Take into account we reviewed it in previous lesson, but instead I will show you examples of consumer
unpracticed.
I prepared real life example for you that will help you to grasp new knowledge faster.
I will leave the link to this file in the lesson attachments.
Imagine that you have a list of products.
This is product type that we used for some of the home tasks.
While we are in collections, let me remind you how it looks like this typesetters contract that allows
us to retrieve Idy product, name, category, name of each product and set price if needed.
In our example, we will use default product type that implements product interface NASSAN special in
the source code of default.
Product type just data was getters and setters.
And our example.
I created a few test products.
I passed the constructor iji product name, category, name and price.
Hobbs's is clear for you.
Now imagine that I want to give possibility for administrator to increase price of products.
For example, administrator could select the specific products.
Those products will be grouped in the least object on the backend and price will be increased for each
product from this list by amount of money set by admin.
Here, a graded method increase price for product list that takes as arguments and list of products
and specific amount of dollars to increase price and a massive body attack.
Iterator of my product list and call for each method.
Let me open the source code of this method to explore it together with you.
As you can see, this is the default method declared in iterator type.
It takes consumer as an argument in simple words.
It takes some actions.
It has to be performed with each element remained in this iterator.
You can see that we have while loop here and accept method of consumer is invoked for each element from
this iterator.
Now let's get back to our demo file and we will implement lambda function for our consumer.
Here you can see the anonymous function that takes one argument and that returns nassan.
I call set price Masset on each product and set the result of radiation of current price and local variables
that passed to the Massett.
And then my program.
I can call this method and pass the list of products an amount of money to increase price.
After that, let me print each product from the new line.
I ran the program here.
You can see in console output that price is increased by ten dollars for each product.
Can you understand now how we modified state for all products in connection with the help of consumer
that we passed two for each remaining MassArt?
Hope it makes sense, by the way, I believe from the previous Lasley already understood that this lambda
function is interchangeable with MassArt reference.
Please let me know in case there are any questions.
When you understood how consumer works, let me show you how my consumer works.
I open source code of BI consumer interface.
We learned with you specifics of naming convention individual function package during the previous lesson.
Now you can understand that this by prefix is associated with the Arati of accept function.
You can see that accept takes two arguments and returns nassan.
This interface also declares one default Masset, and then that is similar to other interfaces from
this package to compose new functions on the basis of already existing functions.
Now, I want to get back to our demo file to show you an example for our next demo.
I created an object of type hash map.
It is parametrized by integer and product type imagines a similar case.
I have product ideas that are mapped against the products and I want to have opportunity to update each
entry in map for that purpose.
I have created increased price for product map method, the text map of ideas and products and price
to increase.
Here you can see that I call for each method on my map object and pass by consumers there.
Here's lambda function that takes two arguments and returns.
Nassan, in case I don't need to modify key, I just ignore it.
And you can see the body of this function is similar to the previous one.
The only difference here is that I have access to the key in case I need it.
Now, in my program I can call the method and parse, map, object and price to increase.
After that we print each product from new line in console and then cancel output.
You can see that price is increased knowing the difference between consumer and by consumer.
You can use either of these types during the development.
Also, you saw how to investigate the matter signature to understand what type is expected.
Usually it is obvious what will be passed to the function as arguments, but in case it is not clear,
you can investigate the source code of matter that requires function.
One of the goals of this course is not to teach you all possible things, but to teach you how you can
learn these things by yourself.
Let me show you how you can understand what values will be passed to the anonymous function during the
runtime.
For example, I open the source code for each method in map type.
The first thing I want to know is where access method is invoked on the buy consumer argument.
Here is MassArt invocation and I see that K and V variables are passed.
But what object stands behind K and V variable based on the variables name?
It is clear what key and value are referencing to.
But just in case we can check it, few lines above we can see that K variable is initialized with key
value on each iteration and variable is initialized with the mapped value.
You can see that operation is performed for each entry.
That's how you can easily understand what values will be passed to the function.
OK, now when we learned consumer and by consumer types, I want to learn with your predicate and by
predicate types.
I believe that knowledge of Lambda functions and Massata references will help us to learn predicate
type faster.
Let me open the source code of Predicate Interface.
Prickett has one abstract method with the name test.
This method takes an argument and returns.
Boolean value is a true or false in stream API.
Predicates are used very often to filter values and remove elements.
For example, to help you understand how it works.
Let me show you a simple example.
For pre-cut example, I created a separate file.
I will also leave link to this file in attachments.
Does lesson.
Imagine that you have a list of products here.
You can see product list that are similar to one we have in consumer demo file and we have to remove
all products from this list that have price more than you specified to this requirement I created to
remove products if prices more than Massett.
That takes a list of products and price as arguments on my list object I call remove if MassArt let
me open source code of this method.
As you can see, this method is from collection interface and it takes Birckhead as an argument.
Test method will be invoked for each element in this list and in case that Masset will return true,
then this element will be removed.
Based on this, we can make a conclusion that we need to write a function that will return true for
elements that must be removed.
Is it clear now let's get back to our demo file.
I wrote function here as it returns.
True, in case product price is more than price value that was passed to this method.
And in my program I call the new method and pass list of products and one hundred.
That means that all products that have price more than one hundred will be removed.
We can see that I have only one product with price more than one hundred.
That's why when I print all product to console from the new line here, I still expect to see the rest
two products.
Let me run the program by some console output.
We can make a conclusion that product with price more than one hundred.
Was removed from my list.
Awesome.
Now let's investigate the default and static methods from project interface it has and method that allows
us to compose two functions and add additional predicate.
This gives us a similar flexibility that we learned in lesson about functions when we combine different
functions.
Let me show you the next example in the source code.
Imagine that I need to remove all products that have price more than some specific value and category
name is Category one and create a Masset where I create two functions.
The first one is already familiar to you.
That function is to test the price and the second function checks category.
And when I call remove if MassArt, I create the new function that is returned as a result of and Masset
invocation.
I combine these two predicates now aliment will be removed only in case requirements of two predicates
will be met.
In my program I pass list of products, price and category one name.
Based on the current state of my product list, I can see that only one product will be removed after
the Macit invocation product with i.e. one has price more than 90 and category name is equal to category
one.
And in console output you can see that the product was a new one was removed.
And I want you to understand that you're going to compose multiple predicates together, not only to
but even more if you need.
That will allow you to create customized predicates, one that will be needed in case we want to compose
predicates to receive predicate that will return.
True, in case we meet the requirements of at least one predicate of combined ones that we need to use
default or method from predicate interface.
I believe that is logical and clear and does not require any additional demo.
The next default method and predicate interface is negate it returns logical negation of the current
predicate.
Is it clear?
Let me show you this on example.
In case in our previous example I would call Negate Masset, the result would be opposite.
That means in case this function returns true, as a result, the force will be returned after negation.
Let me call negate method here and run program one more time.
And in console output you can see that product with ADC was removed.
So the product that is less than 90 and the product that has category names that is not equal to Category
one was removed.
Can you understand now what it is?
If yes, then let's proceed.
We have two more static methods.
Here is equal.
MassArt returns use a predicate that will check if two arguments are equal to make sure that predicate
will work as expected, we need to check that equal Masset in the type that we want to use for comparison
is overeaten.
This is needed because for comparison will be used objects equal MassArt and the second method is not.
That takes expected as an argument.
This method negates the result of the supplied predicate.
You can see that this is achieved by using the call of Landgate method inside.
So technically you can use is an agreed method in case you have variable of type predicate.
Like I showed you an example, one in case you don't have variable of type predicate you can call predicate,
not method and pos lambda function.
Here to help you understand these two masses, let me show you an example in our perfect demo file.
We have one more example at the bottom here.
Imagine that we have a list of users I use user for hash tables type that implements user interface.
I use exactly Zeze implementation because it as equals Masset overridden.
We added a few users to the list.
You can see user i.e. first name, last name, password and email.
That's just for the sake of example.
And according to business requirements, I have to be able to filter the list of users to leave their
only user that I need here.
I created a user object that will be used during filtering.
I call remove if MassArt on my list of users and I call Predicate is equal method here.
But in case I would call just this method, I will get protocols that will remove only user that equals
to this one.
I need exactly the opposite.
I need to keep only this user.
That's why I have to use negation here.
And technically I could call negate method here, but also I can pass this packet to static method not
is it clear.
And when I print the list of users to cancel, I see that I have only users that I wanted to keep in
the list also now you know.
All matters from PAGAD interface and the very last things that I wanted to discuss with you is my predicate
interface.
I believe you already understood how pre-cut works.
The only difference is that test Masset takes.
Two arguments here will use this method when we all stream API.
But the principle of writing and using by predicate is similar to examples that we saw with predicate
beside.
One abstract matter of this interface also has three more default methods Ziya and negate and or methods.
We have just reviewed similar message from pre-record interface.
That's why I think that there is no need to review examples with these methods.
But let me know in case you have any questions.
That's all what I wanted to share with you today.
Now let's recap what we have learned today.
Today we learned with your consumer and by consumer interfaces and real examples, you saw how you can
create LAMDA functions for these types.
After that, we learned precut interface nuance.
All mascot's from pre-record interface and had practice exercises with this type.
And at the end of the lesson we talked about biproduct interface.
That's all for this lesson.
Thanks a lot for your attention.
See you in the next lesson.