Hi Everyone, welcome come back to the tutorial. In this tutorial we will code and explore the consumer functional
interface. The consumer functional interface and all the new functional interfaces that got introduced
as part of Java 8 are available in the java.util.function
java.util.function package.
Let's go ahead and explore that first.
I'm going to press the shift key twice. Its going to give me this search everywhere window. In here I'm going
to search for consumer interface.
There you go!
It has an "I"
icon in it. That means it's an interface. This is the interface which we're going to explore as part of this tutorial.
tutorial.
Let's go ahead and check that. If you take a look at the consumer function interface.
It has the @FunctionalInterface annotation which means this is a functional interface.
Next step is if you take a look at it has one method called accept().
It just accepts an input.
It doesn't return any output out of it.
And then it has another method
called default.
As I mentioned in the previous tutorial. We'll be exploring more about the default in the upcoming
tutorials.
For now, there is another method which
is part of this functional interface but
we are going to explore this method also as part of this tutorial.
Let's see what are the different other interfaces which are part of this java.util.function
java.util.function package
I'm going to click on this icon as you see here this icon is going to take you to the source.
If you take a look at it this is part of the rt.jar file
and if you take a look at it there is a package called Java.
There is another package called "util" and there is another package called "function".
All these new interfaces, whatever you see here starting from this BiConsumer and that ends with UnaryOperator.
All these things are introduced as part of the Java 8.
All these are categories as Functional Interfaces.
In short the implementation of consumer is that accept an input and perform some operation on that input.
Lets go ahead and code this one.
So first step is I am going to create a package called
functionalinterfaces.
And then I am going to name this class as ConsumerExample.
Lets make this class executable by adding the public static void main(). So we have the class ready
to run.
Let's implement the functional interface by using lambda expression.
First step is that Consumer and the type that it is going to accept is String
assign a variable. The use case is that this is going to accept the string as an input and then it is going
to perform an uppercase operation and then print the value in the console.
So open braces () the input type
we are assigning a reference variable as "s"and the type is string and then we're going to perform
the print operation in the console and then we are going to perform that uppercase operation to the input
that is being passed to the consumer functional interface.
Now we have created the first implementation of consumer using lambda.
The next step is that we're going to learn about how to process or how to pass an input to this Functional interface
Functional interface and how it is going to perform the print and uppercase operation on the input that we are going to pass. So
use the reference variable that we created which is "c1". After you press the "." we have two methods one
is accept() and one is andThen() . Using the accept method we are going to pass the input first "accept()" and then
pass the String Java8. Lets check the result
I am going to run this program.
And we will check the result in the console. So it printed the value JAVA8.
But if you look at the text it performed uppercase operation whatever we instructed this consumer functional
interface to do.
Now let's go ahead and implement some real world examples in the next tutorial. With this we came to the
end of tutorial.
Thank you for watching !