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