WEBVTT

0
00:00.400 --> 00:06.350
Hi Everyone, welcome come back to the tutorial. In this tutorial we will code and  explore the consumer functional

1
00:06.470 --> 00:12.860
interface. The consumer functional interface and all the new functional interfaces that got introduced

2
00:13.190 --> 00:16.690
as part of Java 8 are available in the java.util.function 

3
00:16.730 --> 00:18.820
java.util.function package.

4
00:18.960 --> 00:21.360
Let's go ahead and explore that first.

5
00:21.410 --> 00:27.110
I'm going to press the shift key twice. Its going to give me this search everywhere  window. In here I'm going

6
00:27.110 --> 00:30.220
to search for consumer interface.

7
00:30.220 --> 00:30.830
There you go!

8
00:30.830 --> 00:31.440
It has an "I"

9
00:31.460 --> 00:38.500
icon in it. That means it's an interface. This is the  interface which we're going to explore as part of this tutorial.

10
00:38.530 --> 00:39.060
tutorial.

11
00:39.230 --> 00:43.690
Let's go ahead and check that. If you take a look at the  consumer function interface.

12
00:43.820 --> 00:49.820
It has the @FunctionalInterface annotation which means this is a functional interface.

13
00:50.000 --> 00:54.570
Next step is if you take a look at it has one method called accept().

14
00:54.710 --> 00:56.490
It just accepts an input.

15
00:56.570 --> 00:59.660
It doesn't return any output out of it.

16
00:59.900 --> 01:00.650
And then it has another method 

17
01:00.660 --> 01:02.460
called default.

18
01:02.720 --> 01:07.820
As I mentioned in the previous tutorial. We'll be exploring more about the default in the upcoming

19
01:07.820 --> 01:08.700
tutorials.

20
01:08.830 --> 01:10.710
For now, there is another method which

21
01:10.830 --> 01:13.020
is part of this functional interface but

22
01:13.050 --> 01:18.370
we are going to explore this method also as part of this tutorial.

23
01:18.560 --> 01:23.140
Let's see what are the different other interfaces which are part of this java.util.function

24
01:23.180 --> 01:25.090
java.util.function package

25
01:25.410 --> 01:30.730
I'm going to click on this icon as you see here this icon is going to take you to the source.

26
01:30.780 --> 01:35.360
If you take a look at it this is part of the rt.jar file 

27
01:35.630 --> 01:38.410
and if you take a look at it  there is a package called Java.

28
01:38.630 --> 01:42.760
There is another package called "util" and there is another package called  "function".

29
01:43.070 --> 01:49.640
All these new interfaces, whatever you see here starting from this BiConsumer and that ends with UnaryOperator.

30
01:49.640 --> 01:55.120
All these things are introduced as part of the Java 8.

31
01:55.490 --> 02:01.320
All these are categories as Functional Interfaces.

32
02:01.420 --> 02:09.490
In short the implementation of consumer is that accept an input and perform some operation on that input.

33
02:09.660 --> 02:10.800
Lets go ahead and code this one.

34
02:13.480 --> 02:16.650
So first step is I am  going to create a package called 

35
02:17.800 --> 02:19.180
functionalinterfaces.

36
02:22.890 --> 02:25.800
And then I am going to name this class as ConsumerExample.

37
02:33.330 --> 02:41.030
Lets make this class executable by adding the public static void main(). So we have the class ready

38
02:41.060 --> 02:41.700
to run.

39
02:41.840 --> 02:46.390
Let's implement the functional interface by using lambda expression.

40
02:46.520 --> 02:52.570
First step is that  Consumer<String> and the type that it is going to accept is String

41
02:52.670 --> 02:58.640
assign a variable. The use case is that this is going to accept the string as an input and then it is going

42
02:58.640 --> 03:03.770
to perform an uppercase operation and then print the value in the console.

43
03:03.810 --> 03:06.690
So open braces () the input type

44
03:06.680 --> 03:13.960
we are assigning a reference variable as "s"and the type is string and then we're going to perform

45
03:15.410 --> 03:21.870
the print operation in the console and then we are going to perform that uppercase operation to the input

46
03:21.990 --> 03:25.800
that is being passed to the consumer functional interface.

47
03:25.950 --> 03:31.060
Now we have created the first implementation of consumer using lambda.

48
03:31.230 --> 03:37.430
The next step is that we're going to learn about how to process or how to pass an input  to this Functional interface

49
03:37.430 --> 03:45.230
Functional interface and how it is going to perform the print and uppercase operation on the input that we are going to pass. So

50
03:45.240 --> 03:51.880
use the reference variable that we created which is "c1".  After you press the "." we have two methods one

51
03:51.900 --> 03:59.400
is accept() and one is andThen() . Using the accept method we are going to pass the input first "accept()" and then

52
04:00.440 --> 04:03.970
pass the String Java8. Lets check the result

53
04:04.130 --> 04:06.160
I am going to run this program.

54
04:06.810 --> 04:12.470
And we will check the result in the console.  So it printed the value JAVA8.

55
04:12.560 --> 04:20.030
But if you look at the text it performed uppercase operation whatever we instructed this consumer functional

56
04:20.030 --> 04:22.190
interface to do.

57
04:22.190 --> 04:28.130
Now let's go ahead and implement some real world examples in the next tutorial. With this we came to the

58
04:28.130 --> 04:29.310
end of tutorial.

59
04:29.450 --> 04:30.350
Thank you for watching !