Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about the flatMap function which is part of the streams API.
Basically the flatmap is similar to the map function.
It is going to transform from one type to the another type, or it can transform from one form of the
same type to another form of the same type, but it is used in the context of stream, where each and
every element in the stream represents multiple elements.
So examples are if you have a stream of list or stream of arrays, basically each and every element
in the stream represents a list.
Each and every element in the stream represent arrays.
Because we all know that lists are arrays can have multiple elements.
I just provided arrays and list as an example.
It can be any type of collection that represents multiple elements.
Let's go ahead and code this one.
I have the IntelliJ open.
What I'm going to do is in the streams package, I'm going to create a new class called Streams flatMap
example.
Streams.
Map.
Example, I'm going to make this class executable by adding public static void main.
There you go.
Let me put this in presentation mode.
So now what I'm going to do, let's go ahead and visit the student database class.
Student database.
In here.
If you take a look at it, the student database has name, grade level GPA, gender and the activities.
These are the activities which represent a list.
What I want is I want to print all the activities in a list.
That is my goal.
So I'm going to use the Streams API in order to perform that activity.
So I'm going to create a method called.
Public static.
So activities are of type string, list of string and then print.
Student activities.
I'm gonna.
For this.
There you go.
Good.
Now what I'm going to do, I'm going to use a student database class, and then I'm going to call the
get all students.
This is going to give you the list of all the students that are available as part of the student database.
I'm going to call the stream method, which is going to create a stream.
So the next step, what is that I'm going to do?
I'm going to do map of.
You don't.
So what is that we want?
We want the activity.
Student get activities.
Right.
Okay, so.
Let's write the code comments here.
Basically, this is going to give you a stream of student.
Right, That's good.
And then we are mapping the student to this list of string meaning, which has all the activities.
This is going to give you a stream of list of.
Think so.
Each and every element in the stream is going to be a list of string.
Now, if I try to do a collect, right, so let's say I'm going to do a collect.
Of tallest.
So this is what we have done before.
If we just want to collect the names, we just do this.
What?
Static method?
Okay.
And then let's do the.
List of string.
Going to be student.
Activities.
Now, if we.
As soon as I did that.
Let's return this one to return.
Student activities.
So here it is, complaining.
Right.
Let's go ahead and take a look at that error.
No instances of type variable exist.
So that list of string conforms to string.
Basically, it is looking for a stream of string, not the stream of list of string.
That's when the flatmap comes into picture.
So here each and every element in the stream is going to reference a list of string.
In those kind of scenarios you need to use flatMap.
So I'm going to use flatMap.
Basically, we have to flatten that stream.
That's the actual purpose that it is going to do behind the scenes.
And then we are going to do list.
Colon.
Spring.
So we all know.
So here also I'm using a list we're calling a stream method.
It creates a stream of student here.
This is a list of string.
When you flatten it by calling the flatMap, it is going to create a stream of string here.
Now.
It will be able to collect each and every element in that activities.
List.
Now what we will do, we will print.
Okay.
So I'm going to call.
Gonna have the sea salt.
You typing this out?
It's going to give me the shortcut.
I'm going to give the print student activities.
And I'm going to call this method.
Now, if I run this, basically it is going to give me all the.
List of activities that students are participating.
And let's do a quick recap of what we did.
So flatMap.
We have used Map and flatMap here, actually.
So flatMap is used in scenarios where each and every element in the stream, like in this example,
it represents a list.
Let's say if we have a set, you can still use set colon.
Colon string.
So in those kind of scenarios, you will use flatMap instead of a regular map.
That's the one thing which we have to register in our mind.
With this.
We came to the end of the tutorial.
Thank you for watching.