WEBVTT

0
00:00.440 --> 00:05.440
Hi everyone welcome come back to the tutorial. In this tutorial we're going to code another example to

1
00:05.440 --> 00:11.560
showcase the difference between the imperative style of programming and declarative style of programming.

2
00:11.800 --> 00:16.350
The use case is to remove the duplicates from a list of integers.

3
00:16.360 --> 00:19.830
Let's code this one. I have the IntelliJ open.

4
00:19.950 --> 00:32.420
I'm going to create a class called ImperativeVsDeclarativeExample2. I am going to make this class executable

5
00:32.420 --> 00:35.090
by adding a public static void main method.

6
00:39.060 --> 00:42.560
First step is we are going to create the input list.

7
00:42.900 --> 00:44.580
Going to be the list of Integers.

8
00:47.840 --> 00:55.640
and then lets give it a name in integerList. Lets import this list.  It's going to be I'm going to use the Arrays

9
00:55.640 --> 01:10.030
class dot as Arrays.asList() method and then I'm going to pass the put values 1 2 2 3 3 4 5 5 6 7 8 8 9 and 10.

10
01:10.070 --> 01:19.050
So the use case is  we want to remove all the duplicates from it and then have the list with the unique values.

11
01:19.730 --> 01:25.460
If we have to code this in a imperative approach,  how do we do it? The way we do it is. Let me give some code

12
01:25.460 --> 01:29.160
comments here. First step is we are going to do the imperative approach. 

13
01:31.420 --> 01:36.160
Followed by that we are going code the Declarative approach.

14
01:38.810 --> 01:44.430
In the imperative approach, First step is I am going to create a list.

15
01:44.500 --> 01:53.260
This is going to hold the output. I'm going to name it as a unique list and then lets initialize this.

16
01:54.130 --> 01:54.580
There you go.

17
01:54.630 --> 01:56.760
Now we have the unique list available.

18
01:56.870 --> 02:05.950
The next step is I am going to iterate this list Integer integer : integerList.

19
02:06.270 --> 02:19.520
And what I'm going to do,  I'm going to check if uniqueList.contains(integer) . If the list contains the integer

20
02:19.520 --> 02:22.650
then that means that value is already there

21
02:22.670 --> 02:24.680
Then I want to skip that value. Right ?

22
02:24.770 --> 02:27.850
So this not condition is going to help me achieve that.

23
02:28.160 --> 02:36.540
And then I'm going to add only  if that integer is not part of the unique list and then I will add this.

24
02:36.540 --> 02:43.480
This is the Imperative style. Basically we iterate the list and we check for it basically to find out whether

25
02:43.510 --> 02:45.850
that element is part of that unique list or not.

26
02:45.990 --> 02:52.910
If it is not there that means it is a unique value and we will add that value to this list.

27
02:52.980 --> 02:54.240
I'm going to print this value.

28
02:57.570 --> 03:04.860
There you go ! And run this and check the result.Going to run this program.

29
03:05.850 --> 03:08.240
Gave the unique list here.

30
03:08.520 --> 03:14.070
Let's see how we can achieve the same logic in a declarative way.

31
03:14.320 --> 03:18.750
Basically we are not going to write the logic on how to find the uniqueness.

32
03:18.760 --> 03:24.680
We are going to make use of the functions that are already present as part of the Java8.

33
03:24.730 --> 03:29.920
Basically Java8 is released with a bunch of functions and lots and lots of functions are available.

34
03:29.920 --> 03:36.700
We just have to find the right function and then use it. This course is going to help you understand all the

35
03:36.700 --> 03:40.030
functions that are released as part of Java8.

36
03:40.270 --> 03:46.750
Now the first step is I'm going to use the source the source is going to be the integer list and then

37
03:46.780 --> 03:51.840
if you take a look at it there is a new method called stream() that got introduced as part of Java8.

38
03:52.390 --> 03:55.670
And what we're going to do was we are going to call a method called distinct.

39
03:55.720 --> 04:02.650
This is like in SQL we perform the distinct in order to get the unique set of values from the table.

40
04:02.950 --> 04:04.520
It's going to be a similar concept.

41
04:04.720 --> 04:07.050
And then we want to collect the values right.

42
04:07.060 --> 04:11.600
We want to collect. There is something called collectors.

43
04:11.600 --> 04:13.640
How do you want to collect what collection you want ?

44
04:13.640 --> 04:17.580
I want to collect as a list.

45
04:17.750 --> 04:19.880
Now I'm going to give it a name.

46
04:20.150 --> 04:22.000
List<Integers>

47
04:25.480 --> 04:33.090
and integer list one  or we can name it as uniquelist1. I'm going to print it here.

48
04:37.040 --> 04:41.610
There you go. Now let's run this and verify the result.

49
04:43.890 --> 04:46.740
There you go,  t gave you the same result as this one.

50
04:46.820 --> 04:52.250
But if you take a look at the code readability wise  here we are just calling some functions we

51
04:52.270 --> 04:58.500
don't worry about how it is implemented. The system or the functions that got released as part of the language.

52
04:58.510 --> 05:03.290
It takes care of performing the operation that you requested and it gives you the result.

53
05:03.460 --> 05:10.030
So this is one of the advantage of the declarative programming. We just call the appropriate functions

54
05:10.270 --> 05:15.550
based on your use case and it is going to perform the work and give you the result that you are looking

55
05:15.550 --> 05:16.120
for.

56
05:16.360 --> 05:22.440
So all you have to do is build your knowledge on the functions that are available and use them as per

57
05:22.450 --> 05:24.150
your use case.

58
05:24.250 --> 05:27.130
But this we came to the end of this tutorial. Thank you for watching.