Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Mapping allows you to transform each element to a new value.
Learn more
-
0:00
So we now have the ability to take a collection of items, and
-
0:04
gather a portion of those into another collection.
-
0:07
That works well, but a lot of time in the reality of our code base,
-
0:10
what we are actually needing to do is collect our subset of items, and
-
0:14
then produce different representations of those same items.
-
0:19
For instance, maybe we want to populate a drop down list in an application,
-
0:23
with just a name of those items.
-
0:25
Maybe we want to produce some mastered detail representation on our webpage,
-
0:28
we're we show a paginated list of items as links.
-
0:31
And clicking on that link will go to a more detailed page.
-
0:35
Streams add some real power, because instead of making a separate collection of
-
0:39
the items, you can actually transform the item
-
0:41
as it's passing through your functional pipeline, in one foul swoop.
-
0:45
It's pretty powerful.
-
0:47
There is a bit of a naming collision unfortunately, so
-
0:50
the method name that we're going to use on our stream is named Map.
-
0:55
Now this unfortunately, is the same word that we use for
-
0:58
our key value data structure, map.
-
1:00
Like a HashMap.
-
1:02
And I'm bringing that up now, so you don't confuse the two.
-
1:06
The map is used to transform or map one item to another one.
-
1:12
So, similar to how filter used the predicate functional interface,
-
1:15
map uses the function functional interface.
-
1:18
The single abstract method is named Apply,
-
1:21
and the signature is that it takes a single value and returns a value.
-
1:26
Ready?
-
1:26
Let's put the functions on the map.
-
1:29
Bad idea to throw in another map there.
-
1:31
Sorry about that.
-
1:33
So the job output is a little tacky currently.
-
1:37
So, let's do this.
-
1:38
Let's assume that we have a page in our application,
-
1:41
where we want to give an example of the jobs available.
-
1:44
So, let's show off three junior jobs but let's format that a bit prettier.
-
1:50
So imperatively, let's do this.
-
1:52
Let's copy the imperative method that we used to get the first three.
-
1:56
So let's copy that and then paste it.
-
1:58
So we're gonna copy it, give us some space here, copy this.
-
2:06
And let's paste it here.
-
2:09
And, so now let's go ahead and let's rename that
-
2:14
to getCaptionsImperatively, so we'll pull a caption out there.
-
2:18
So getCaptionsImperatively.
-
2:20
And instead of returning a Job, we'll return a String this time.
-
2:24
A List of strings, so
-
2:26
the caption instead of that long string mess there, will make it prettier.
-
2:30
So now, this is complaining we need this to be a String.
-
2:34
I'm going to go ahead, I'm going to rename this refactor rename.
-
2:42
And we'll call this captions.
-
2:47
So let's make a pretty caption in here so we'll say, String caption and
-
2:54
we'll d a nice format string, so we'll do format, and
-
2:59
we'll say the company is looking for a Job title and location.
-
3:04
That's a pretty nice caption.
-
3:08
So let's see.
-
3:09
Let's break that out a little bit.
-
3:12
So the first present s is going be job.getCompany,
-
3:17
and the next will be job.getTitle.
-
3:21
And finally, we'll do a Job.getCity.
-
3:27
Go ahead and supply add a caption here.
-
3:29
Cool, and then it will return the captions.
-
3:34
So we now have getCaptionsImperatively, so let's go use it.
-
3:38
Let's make sure it looks pretty.
-
3:41
So we'll say get.
-
3:44
CaptionsImperatively, we'll run the jobs through there and let's take a look.
-
3:48
Beautiful, looks a lot better.
-
3:51
Now, let's see what this looks like stream wise.
-
3:54
So that was here, we'll get these three junior jobs.
-
4:00
And I'll move this down so that we can keep them together.
-
4:03
Say.
-
4:07
GetThree, let me change this to be captions.
-
4:13
GetCaptions.
-
4:18
Stream, and of course we also need to change this to now be a list of Strings.
-
4:24
Now, check out this error that we've created.
-
4:26
See how this list here knows that it's supposed to be returning a String,
-
4:30
but in fact we are returning a Job.
-
4:32
So, we need to transform our Job into a String, and
-
4:36
we do that with the map method.
-
4:39
So, let's go, let's Job, let's do it.
-
4:40
So we will come here.
-
4:43
So we're sure that we have a JuniorJob, then we're gonna do a .map.
-
4:47
And you'll see that it takes a function.
-
4:50
So if we take a lambda, we'll be getting in a job, a JuniorJob for
-
4:54
sure because it's been focused that way, and
-
4:56
then we will just take that same code that we did here.
-
5:00
What we did here the String format will.
-
5:07
Remember, it will automatically return a String.
-
5:11
I don't have an extra one in there.
-
5:16
Let's bring this up here, and then we'll close our map out, there we go.
-
5:20
And if we come up to our example, our explore up here and
-
5:24
we change this to Stream, let's see how we did.
-
5:32
Awesome, but we've duplicated this code.
-
5:35
And we've duplicated this string format code.
-
5:37
Now, chances are we're not gonna be the only ones needing it.
-
5:41
So, we could make a static method or why don't we do this,
-
5:45
let's make a computed property on our job object.
-
5:48
It seems like others might want to do this.
-
5:51
So I'm going to grab this from here.
-
5:55
We'll cut this out, and
-
5:59
let's just say job.getCaption.
-
6:03
And it won't exist, and then we go create it.
-
6:08
So we create for that.
-
6:11
And this is going to return a String.
-
6:17
And it's going to return the String.format.
-
6:20
Get rid of these.
-
6:24
Jobs here, and put a semi-colon.
-
6:27
So now, we have.
-
6:29
What we need.
-
6:31
And over here, it will return job.getCaption.
-
6:34
And let's go ahead, and we'll clean up our imperative one.
-
6:37
So, what we'll do is we'll say captions.add.
-
6:45
Job.getCaption.
-
6:48
There that is prettier.
-
6:51
So now if a job passes through and it is a JuniorJob, it's gonna come into map.
-
6:56
Map is going to take a Job, and it's going to return a String.
-
7:01
So it's going transform into the String of the caption, and
-
7:03
then it's going to go through.
-
7:05
And that's the apply method of function.
-
7:07
It's going to take a type, and it's going to return a different type.
-
7:11
Or whatever, it's going to return something else.
-
7:14
So, it took in a Job, it returned a String, and
-
7:16
we know that it's a String now.
-
7:18
So look here, there's an intention action light bulb, let's go ahead and
-
7:21
see what it says, it says replace lambda with method reference.
-
7:23
Good, we love method references, let's do that.
-
7:26
Wow, [LAUGH] hat looks great.
-
7:28
But wait a second, getCaption on the Job class doesn't take a parameter.
-
7:33
It doesn't take a parameter, it's just empty, so how is that working?
-
7:39
I mean isJuniorJob, this method here,
-
7:43
isJuniorJob, that takes a Job and returns a Boolean.
-
7:49
But, getCaption doesn't.
-
7:51
Now this is a common head scratcher, and
-
7:54
the reason is because it depends of the type of method.
-
7:58
This isJuniorHob, this is static.
-
8:03
However, this getCaption method is on a Job instance.
-
8:09
You can't execute the method without an instance of a job.
-
8:12
Method references know that, so
-
8:14
the implicitly require an object to execute the instance method on.
-
8:19
So really, you can picture any method reference that is referring to an instance
-
8:24
method, like our getCaption method here as a function that expects an argument
-
8:29
that is the instance of the type that has that method on it.
-
8:34
So Job, it's always assuming a Job is passed to it.
-
8:39
So, the Job that comes through this filter is passed in here.
-
8:43
And getCaption is run on that Job.
-
8:46
Otherwise, how can the method run?
-
8:48
If that wasn't clear, go ahead and rewind me a bit.
-
8:50
It's a little strange of a concept, and it's okay for
-
8:53
it to take a while to sink in.
-
8:54
You'll see this quite a bit when map is used.
-
8:56
Especially when trying to just get a getter,
-
8:59
transforming an object to one of its properties.
-
9:02
Actually, that is known as method reference inference, and
-
9:05
we can cross that off our parking lot.
-
9:07
So let's go over there.
-
9:08
So Method Reference Inference, let's go ahead and
-
9:11
we will do a Strikethrough, perfect.
-
9:15
So, let's take a quick break and when we return we'll talk about what happens
-
9:20
when you encounter a value that maps to a stream from within your streams.
-
9:26
Yo dog, I heard you like streams in your streams.
You need to sign up for Treehouse in order to download course files.
Sign up