Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Ranges6:11 with Craig Dennis
You can use a stream to provide a range of numbers. This helps when you need to keep track of an index or process a series of numbers.
All this internal iteration feels pretty great, and 0:00 it's really nice to not have to think about iteration at all. 0:03 Then inevitably there comes a time when you actually miss your old school 0:07 for-loop. 0:11 We developers have solved a lot of problems with that old trusty for-loop. 0:12 And that for-loop really shines when you need to keep track of an index 0:16 inside of a loop. 0:20 Well lucky for us, there's a way to do that and more, and 0:22 still keep using our streams. 0:25 Okay, so the first thing that comes to mind for me is a console-based menu. 0:27 You know the type where it lists some options for you and 0:31 you choose the option by entering a number. 0:34 So, let's do that. 0:37 Let's find all the distinct companies in our list of jobs and 0:38 then provide a menu for someone to choose from. 0:41 So let's make a separate List. 0:44 So, let's get rid of this code here, and 0:45 we will make a List of Strings, and these will be company names. 0:49 So I say companies, then we'll do jobs.stream. 0:54 Okay, and from here we're going to map the company, so we'll do Job: :getCompany. 0:59 Okay, so we want to have a distinct amount of these companies. 1:04 We want the company that will only show up once. 1:09 So distinct is one of those stateful intermediate operations and 1:11 it keeps track of everything that comes through. 1:16 So you just call it, distinct. 1:18 There we go, so now, if IBM comes through twice, it's only gonna be there once. 1:20 Cool, right? 1:24 And we should probably sort the List, right, so that it'll be easier to find. 1:25 So that's pretty easy too, sorted. 1:28 Now the trick there of course is that we're working with alphabetical things, 1:30 and it's just these words, and they're gonna be sorted in alphabetical order. 1:34 Pretty nice, right? 1:38 And then finally, let's save that to a List, 1:39 right, so we'll do collect and Collectors.toList. 1:40 So now that we have our List, let's make a menu imperatively first. 1:44 So we wanna display 20, right? 1:49 That's about the size of that screen there, so we'll display 20 and 1:51 let's do it, let's make that old faithful for-loops, so for, int, i =0. 1:55 We wanna start at 0 cuz that's customary in these loops, right? 2:01 And we wanna get 20. 2:07 So what do we do? 2:08 So it's less than, is it less than, or equal and, or less than 20? 2:11 You always end up doing that, right? 2:15 Every single time. 2:16 And then finally, we're gonna increment here, we'll do i++. 2:17 So then we need to remember to increment our i when we display it, right? 2:22 So let's get a little print formatter out here. 2:26 So we'll do a %d, and I will do a %s, and then a %n. 2:29 And we need to remember to increment that i when we display it, 2:36 we don't want it to be zero dot as an option, because that's too weird for 2:39 humans, only us programmers can handle that. 2:43 So we'll do i + 1, 2:46 and then we're gonna get from this companies List, we're gonna get that i. 2:48 Which is zero-based, right? 2:53 Right, programmers, this one's for humans, for programmers. 2:55 All right, cool, let's give that a run and see how we did. 2:59 All right, nice, looking pretty good. 3:05 It's got the numbers at the top, that's totally fine. 3:06 Three M, stickiest place right? 3:10 Okay, so let's extract this just for reference later. 3:14 So we'll do extract method, and let's call this 3:17 displayCompaniesMenuImperatively. 3:22 So the functional approach follows the same thought process. 3:32 But instead of using a for-loop, we'll use a range. 3:36 So let's just dupe the function definition. 3:40 Let's do this, let's grab this here. 3:42 And we'll drop that right here underneath it. 3:44 And instead of Imperatively, 3:46 we'll say displayCompaniesMenuUsingRange. 3:49 There we go. 3:55 So we'd like a stream of integers, so we'll start with an IntStream. 3:59 And IntStreams have a range method. 4:05 So we'll say range, and you'll see here, the first is startInclusive, 4:07 so we wanna start this at 1, and then it says endExclusive. 4:12 That's the same sort of thing here as the for-loop isn't it? 4:17 I wonder what that means. 4:20 Should we just, does that mean that 20 will show up? 4:22 Let's just put 20 here. 4:24 Let's see what happens. 4:25 We can always explore, right? 4:27 We're gonna explore this. 4:29 So just like how we were able to specialize the stream to turn it into 4:30 an IntStream, we can also flip it back into a regular stream. 4:34 So we have an IntStream and we wanna go to an object. 4:40 So we're gonna map to an object, and here let's just use i, 4:42 just to stay with the index scheme, that's what the i stands for in those loops. 4:46 And here I wanna generate the output, so we'll do String.format. 4:50 And we'll do just like we had up there, in fact, let's just copy that. 4:56 Okay, so we're gonna do a String.format, and in this case we have the i, and 5:04 the i is gonna be starting at 1, so we'll do the reverse here. 5:08 So we'll say companies.get(i- 1). 5:12 And then finally, we will spit it out. 5:16 Cool. 5:24 So let's say, displayCompaniesMenuUsingRange. 5:24 Take a look. 5:30 Shoot. 5:32 And actually we don't need the new line anymore, cuz we're printing that out. 5:34 But look, we only went to 19, that's what exclusive meant, it excludes the top. 5:37 Okay, so there is another method here called rangeClosed. 5:43 So this parameter here is end inclusively. 5:48 Of course, so we'll run that again, and boom, there we go, we got to 20. 5:54 So that is what we need when we have a start and no int end. 5:58 But sometimes you don't know the top half or the end half of the range. 6:02 Let's take a quick break and then explore infinite streams 6:07
You need to sign up for Treehouse in order to download course files.Sign up