Hi everyone welcome come back to the tutorial. In this tutorial we're going to code another example to
showcase the difference between the imperative style of programming and declarative style of programming.
The use case is to remove the duplicates from a list of integers.
Let's code this one. I have the IntelliJ open.
I'm going to create a class called ImperativeVsDeclarativeExample2. I am going to make this class executable
by adding a public static void main method.
First step is we are going to create the input list.
Going to be the list of Integers.
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
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.
So the use case is we want to remove all the duplicates from it and then have the list with the unique values.
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
comments here. First step is we are going to do the imperative approach.
Followed by that we are going code the Declarative approach.
In the imperative approach, First step is I am going to create a list.
This is going to hold the output. I'm going to name it as a unique list and then lets initialize this.
There you go.
Now we have the unique list available.
The next step is I am going to iterate this list Integer integer : integerList.
And what I'm going to do, I'm going to check if uniqueList.contains(integer) . If the list contains the integer
then that means that value is already there
Then I want to skip that value. Right ?
So this not condition is going to help me achieve that.
And then I'm going to add only if that integer is not part of the unique list and then I will add this.
This is the Imperative style. Basically we iterate the list and we check for it basically to find out whether
that element is part of that unique list or not.
If it is not there that means it is a unique value and we will add that value to this list.
I'm going to print this value.
There you go ! And run this and check the result.Going to run this program.
Gave the unique list here.
Let's see how we can achieve the same logic in a declarative way.
Basically we are not going to write the logic on how to find the uniqueness.
We are going to make use of the functions that are already present as part of the Java8.
Basically Java8 is released with a bunch of functions and lots and lots of functions are available.
We just have to find the right function and then use it. This course is going to help you understand all the
functions that are released as part of Java8.
Now the first step is I'm going to use the source the source is going to be the integer list and then
if you take a look at it there is a new method called stream() that got introduced as part of Java8.
And what we're going to do was we are going to call a method called distinct.
This is like in SQL we perform the distinct in order to get the unique set of values from the table.
It's going to be a similar concept.
And then we want to collect the values right.
We want to collect. There is something called collectors.
How do you want to collect what collection you want ?
I want to collect as a list.
Now I'm going to give it a name.
List
and integer list one or we can name it as uniquelist1. I'm going to print it here.
There you go. Now let's run this and verify the result.
There you go, t gave you the same result as this one.
But if you take a look at the code readability wise here we are just calling some functions we
don't worry about how it is implemented. The system or the functions that got released as part of the language.
It takes care of performing the operation that you requested and it gives you the result.
So this is one of the advantage of the declarative programming. We just call the appropriate functions
based on your use case and it is going to perform the work and give you the result that you are looking
for.
So all you have to do is build your knowledge on the functions that are available and use them as per
your use case.
But this we came to the end of this tutorial. Thank you for watching.