Check out a free preview of the full Java Fundamentals course
The "map & reduce" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
Angie demonstrates the stream operations map and reduce, which allow manipulation of the data within the stream. Examples using both strings and integers are provided in this segment.
Transcript from the "map & reduce" Lesson
[00:00:00]
>> So there are also stream operations that allow you to manipulate the data. Again, not in their original source, but within the stream itself, and one of those operation is map. So not map that we saw before, not the data structure map, but map in functional programming, right?
[00:00:22]
So let's go ahead and make a new method and we'll call that one mapDemo. And we're gonna use map to convert every element in the stream to uppercase, all right? So let's say veggies.stream .map, and this map takes a function. So that's the functional interface that it takes, right?
[00:01:03]
So now you're starting to see these functional interfaces in play. Basically, remember the definitions that we had for those functional interfaces. It takes an argument, and it returns something of the same type, or it takes an argument, it returns a Boolean, right? So you start seeing how those functional interfaces come in play.
[00:01:23]
Whatever you put in here is the implementation for its abstract method. So for function, we can just give it a method, so we'll say string, this was a good question earlier. Say, when we use the method reference, do we have to use static methods? No, so I'm gonna use string here and toUpperCase just to give you some variety, okay?
[00:01:56]
So that's the same as if I said V arrow V.toUpperCase and then like gave it So map returns a resulting stream. Basically you start to see like, you can get a feel what is intermediate versus was terminal for each printing things. Why would they return that same thing back to me whereas filter is narrowing the list down, so that makes sense to return that back to me, right?
[00:02:38]
Any match in all matches of boolean, why would they give me the stream back? It's just a yes or no answer. So you start getting a feel of which one of these are terminal, which ones are intermediate? This is modifying the stream in some way. So it's gonna return that modified stream back to me.
[00:02:55]
So let's just go ahead and print it out so we can say for each. And then let's print it out. So system.out::println, okay? Make sure you call it from your main method. So notice I have the entire list printed in uppercase. Again, it did not modify the initial list.
[00:03:31]
This is the stream that was modified, so that is modifying the data that is in the stream, all right? We're gonna do another one. Let's do reduce. So we'll say public static void reduceDemo, yes. What the reduce does is essentially like, don't think of reduced like a math terms, think of reduce as in cooking terms, this is how I think of it.
[00:04:09]
When you're cooking, any chefs in the room, people that cook often? All right, so when you're cooking, some sort of broth or sauce, we say, I gotta reduce it. I wanna kind of basically smush it down or whatever. And that's the same way that this works. So if we were to use, let's just use veggies, and say reduce, yeah, stream.reduce, sorry.
[00:04:39]
Reduce. What you need to do with this is you're going to give it a couple of arguments. So you can give it basically like a binary operator functional interface implementation or you can start off with an identity, I see a lot of times people start with an identity.
[00:05:06]
So starting with the identity is basically like, let me give you the place to start from. So if you were reducing a string, usually that would be empty string. So start with nothing, right? And then let me give you some sort of lambda expression. So the binary operator again, if we remember when we looked at that binary signature, it takes two arguments of the same type, and then it returns back a result of that same type.
[00:05:39]
So here we're gonna need two variables in our lambda expression, we'll say a and b. And we basically do the arrow function and say what do we want to happen with a and b. And a and b in this instance and reduce it would be like the first string and then the second string, okay?
[00:06:02]
You'll have a result, that resulting string in the next part, the next element in the stream. How do you want these combined? How do you want these reduced together? So I can say, all right. I want you to have the first string, and then I want you to add a little pipe in between them, and then add the second string, okay?
[00:06:28]
So what you've done there is essentially reduce the entire stream into one string that is pipe delimited. Based on gut feeling, do you think reduce is the intermediate or terminal?
>> Terminal meaning.
>> It returns a string back to me. That's right. Okay, nevermind. So it does return a string back to me.
[00:07:01]
The stream that we had is squished it together based on how we say we wanted to squish and then say here's your squished TECHSPEC stream, it's no longer a stream, right? Okay, so that was a trick question. I even tricked myself. [LAUGH] I don't have a stream anymore.
[00:07:22]
I have a resulting string at that point, right? So with that string, you could then do whatever you want. So I could essentially print this whole thing out so I can put this whole thing in a print statement for example like such. All right, so let's go ahead and print this out.
[00:07:50]
Let's see what we've done here. All right, great. So notice now is a single string all like, smushed together. So if I wanted to do any, like other operations before reduce, I could definitely do that. So before I call that terminal operation, what if I say, well, first sort of thing.
[00:08:16]
So that'll take that collection, turn it into a stream, sorted in alphabetical order, and then smash it down to an individual string. And then if we print it that, notice they're now in alphabetical order. All right, now I wanted to also show you integers. So that's where I mostly see reduce used.
[00:08:51]
So we'll make another example here. Public static void. And we'll say intReduceDemo. So we're gonna make a list of integers. And I'm just gonna add a couple of even numbers here, And then I can say numbers.stream, Dot reduce, and I mean the arguments for reduce do not change just because it's a number, right?
[00:09:42]
Still the same ones. So I wanna give it an identity, meaning where do I wanna start? 0, and then I wanna give it a binary. Operator, implementation, so again, two arguments. So given the first two numbers, how do you want this reduced? I say, you know what, go ahead and add a to b, If i add a to b, who can tell me what's happening to the entire stream?
[00:10:20]
What would the end result kind of be?
>> It's getting summed.
>> It's summing the whole stream up, right? So I'm saying go through this whole thing, take the first number, add it to the second number. Once it does that, it has a sum. That sum becomes the first number.
[00:10:34]
That becomes a. It goes to the next part, add it, add it, add it, all right? So that accumulates all of the variables. So this is when I really see reduced use with numbers or something like that. And you wanna know the sum of the whole thing or something like that.
[00:10:55]
>> What is that returning then?
>> That would give you an integer variable back, I think. Yeah, so whatever the, remember, it gives you the thing of the same type back. So we gave it integers, it should give us an integer back. Can I assign this? Let's see.
[00:11:14]
See, that's when var comes in and you're like, I don't know what I'm getting back. Okay, var sum. Let's see what happens. Yes, so I got an integer back.
>> And then you could throw that out into like a system out.
>> Yeah, so let's just like print out the value.
[00:11:31]
So if we set out some, right, so I gotta call this method. I think I should get 30 back if my math is right. Okay, let's run this. Yeah, got 30 back, okay?
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops