Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Reduction Operations7:25 with Craig Dennis
You can reduce an entire collection of elements into a single value.
[MUSIC] 0:00 A common task in programming is to process an entire list of elements to produce 0:04 a single value. 0:09 For instance, you might have a list of numbers and 0:10 you wanna know what they all add up to. 0:13 That would return a single number. 0:15 Maybe you wanna find the smallest number in that list or maybe the largest or 0:17 the average. 0:20 The point is you can reduce a collection of things to a single value. 0:22 Now unfortunately, 0:26 the job postings we've been working with don't have salary information. 0:27 Get used to that during the job hunt. 0:31 So we'll have to come up with something else to count. 0:34 We'll find something to reduce. 0:37 Okay, so you can actually have more specialized streams. 0:39 So instead of before we had just a stream like this, 0:44 well there's actually one called an IntStream, all right? 0:47 And so then, of course, you can add primitive ints here in the varargs. 0:50 So we'll say IntStream.of, we'll just give 1, 2, 3, 4, and that is IntStream. 0:55 And this new stream gives you some handy terminal reduction operations, like sum. 1:04 So that will take everything and add that up. 1:11 So let's do this, I'm gonna go ahead and wrap this in a print line. 1:13 We'll just move this up here. 1:19 Let's get this in here, like so, there we go. 1:23 So if I run that, it will add that up, 10. 1:26 And so because it's a stream, 1:30 we can add whatever intermediate operations we want in here, like so. 1:31 We could say filter and it's gonna get a number and 1:35 that number is going to be that number less than 4. 1:40 So that will add up anything less than 4. 1:46 So 1 plus 2 plus 3 is 6. 1:47 So there's also a way to find the minimum value in the list. 1:51 Now this is interesting, it returned what is known as an Optional. 1:57 And that's because, what if this stream was empty, right? 2:02 What's the minimum of nothing? 2:06 So optionals provide a way to state that the value is either present or absent. 2:08 They help avoid null pointers, 2:13 so let's focus on that after we finish exploring reduction. 2:14 So as you can imagine, there's also a max that's also gonna return an optional. 2:18 Because what's the max of nothing? 2:24 And you can also very quickly grab an average. 2:25 Now notice that this returns a Double to take care of the half here, right. 2:30 And again, it's an optional, because what's the average of nothing? 2:35 So what happens if you wanted to take advantage of these powerful reduction 2:40 operations in a normal stream? 2:44 Now since we don't really have any great numbers, let's try to find 2:45 the average number of letters in these potential employer's company names. 2:50 Sound good? 2:55 Okay, so I'm gonna get rid of this. 2:57 Let's do jobs.stream. 2:58 And let's get the title of the company, so first we'll get that. 3:03 So we're looking to use a method reference to get the company, right? 3:06 And streams actually provide the way for 3:11 you to specialize your stream and they come in the form of .mapTo. 3:14 So see there, it says mapTo, and we're going to do a mapToInt. 3:19 And then inside here there's just a normal mapping function, right? 3:23 So we'll do companyName, right, cuz that's what's coming from above. 3:26 And we will do companyName.length. 3:30 And look at that. 3:38 It is suggesting that we go ahead and 3:39 use a method reference, right, cuz we're just taking in a value. 3:41 And then we're calling a length on it, so let's do that. 3:45 So it's just String::length on the company name that comes through. 3:47 And so now we have an IntStream, right, like we just had. 3:50 And so now I can do a thing like average. 3:53 Let's see what the average company length name is. 3:56 I should have left that print there. 4:00 Get that back real quick. 4:02 There we go. 4:08 So on average, the employers have 15ish characters in their name, that's cool. 4:10 Let's see what the longest one is. 4:15 So I'm gonna say, what's the max of those company names. 4:17 71, what? 4:21 Now, I totally wanna see what that is, but how would you do that? 4:24 Since we converted that to an int, 4:27 we're only getting the numbers through the stream. 4:30 So let's back that up a bit. 4:32 So streams themselves also have a max and they require a comparator. 4:34 So let's do that, so let's instead of getting the length right here, 4:40 let's do this max. 4:43 So see, max is now asking for a comparator. 4:45 So it's asking for a comparator. 4:49 And you'll see that the one that it suggests, 4:50 actually right off the top, is this Comparator.comparingInt. 4:52 So let's do that, and Comparator is looking for one of these toInt functions. 4:56 So it's looking for something that will return an int. 5:02 So we just looked at one of those, right? 5:05 That's exactly what String::length does. 5:08 So this is gonna get the company with the longest name. 5:11 Let's see. 5:14 So it's Newtek Technology Solutions, a branch of Newtek Business Services Corp. 5:17 I see why that's why so long. 5:23 Now, that seem to little magical, didn't it? 5:24 If so, I feel you, right? 5:27 So let me show you a harder way of doing this right now. 5:29 So might have seen comparators before, probably you have, right? 5:31 That's how you end up sorting things a lot. 5:36 So the way that comparators work 5:38 is you do something that it takes two objects, right? 5:41 So in this case, we're gonna have company1 and 5:44 company2, that's what it's gonna use to compare them. 5:48 And here in the body, what we'll do is we need to compare these two. 5:51 And the way this compare method works is that if it returns anything negative, 5:57 the first item is considered smaller. 6:02 If it returns 0, they're considered equal. 6:04 And if it returns anything positive, it's considered greater. 6:06 So imagine one of those scales, right? 6:10 If you imagine putting the length of each company's name on each side of that, 6:12 that's what we're doing. 6:16 We're weighing which one of these is longer. 6:17 So with some mathy thoughts, if I subtract the values from each other, 6:20 that should cause us to get the right positive, negative or equal, right? 6:25 So if I say, company1.length- company2.length. 6:29 You'll see if we run it, we'll get the same answer. 6:40 And an even cooler, if we go here and we take a look at what IntelliJ's suggesting. 6:43 It says replace with a comparingInt, and 6:48 it figured out that we're doing the string length. 6:50 Pretty cool, right? 6:52 So this method, this comparingInt here, takes a function and 6:53 returns a function to max. 6:58 Pretty cool, right? 7:01 So functions that do just that are called higher order functions, 7:02 which we already have parked here. 7:06 But you can actually write these powerful functions, too, and 7:08 they're so expressive, aren't they? 7:12 And that is so nice. 7:14 Okay, so 7:16 now that you've seen some of the out of the box terminal reduction operations, 7:17 why don't we explore how to deal with the optional values they sometimes return. 7:21
You need to sign up for Treehouse in order to download course files.Sign up