Infinite Streams4:29 with Craig Dennis
Sometimes you won't know the top end of a range, this is where infinite streams come into play.
A fellow student, David Lin, points out very wisely that I made a common mistake and the code should actually be subtracting 1 from my index.
Thanks for pointing that out David!
So now that you've seen ranges in action, 0:00 you might be wondering how you do the step portion of the for loop, right? 0:03 So this old school for loop here, that's this i++, right? 0:07 So each iteration, what should happen? 0:11 Now in a standard for loop, you just increment your index. 0:13 But you can do other things, maybe you wanna increment it by 2 or 0:17 something like that, right? 0:20 So that's called stepping. 0:21 So this is a little bit of a stretch but I wanna show you how to do that 0:22 declaratively in case you run into that need. 0:26 Okay, so let's say, let's see how many companies we have here. 0:29 How many companies are hiring? 0:35 Over a thousand jobs posted yesterday, let's see. 0:37 657 companies, okay. 0:40 So, that is too many to display in a menu, obviously, right? 0:43 Let's say that we wanna show 20 pages each. 0:48 But you wanna choose from each one of those pages, right? 0:52 So we wanna show every 20th page, what would be the first company on that page. 0:54 And then that go kinda let you choose where in the alphabet to go. 0:58 I know it's a bit of a stretch but it's something fun to play with. 1:02 So in order to do that, we probably wanna say that our page size, 1:05 we'll set a new variable, page size is 20. 1:09 And let's calculate how many pages there actually are, right? 1:12 So we have companies.size and then we'll divide that by the page size, okay? 1:16 And then that way we can change that and this will dynamically fix itself. 1:22 Okay, so, there is a method on 1:26 IntStreams that will generate an infinite stream of values. 1:30 And it's called iterate. 1:35 And what iterate does, is it takes a seed or a starting point. 1:38 So in this case, we're gonna start at 1, just like we did before. 1:42 And then each time it's called upon, it takes the last value, and 1:45 runs it through a function. 1:49 So that function is gonna take an i, we'll just say, i + pageSize, right? 1:51 So each number that this generates the next one. 1:57 So the first one will be 1, the second one would be 21. 2:00 And the next one would be 41, right? 2:03 So it's gonna add 20 each time, that's what we're gonna do. 2:05 So we'll use very similar logic to convert our item as we did before. 2:09 So we'll say, .map.ToObj, and again, that's gonna take an i. 2:13 And we'll do String.format. 2:17 And we'll give it a d, %s. 2:22 And very similar, we'll do i and 2:26 we'll do companies.get(i). 2:30 Now the reason why we have the page size is to make sure that we can limit this. 2:35 Otherwise, it will just keep on running, we need to tell it when to stop. 2:39 So we're gonna limit it to the number of pages. 2:42 And obviously, if it went past the number of pages, 2:45 our companies.get(i) would be out of balance, right? 2:47 Because this is not gonna stop, this is not based on any top level. 2:50 We didn't specify the end of the range. 2:54 We specified an infinite stream. 2:56 And then finally, let's print out our 3:00 Cool, so let's take a look what this looks like. 3:08 Cool, awesome. 3:13 So there, right? 3:16 1, 21, 41, and that's happening because it's going across here. 3:17 So again, I just wanna show you that this is infinite. 3:21 And you can actually create infinite loops in functional programming. 3:25 So check this out. 3:28 I'm gonna do this. 3:29 I'm gonna remove the mapToObj because we obviously, 3:29 we don't want that to go out of bounds. 3:32 And I'm also gonna remove this limit here. 3:33 I'm just gonna go ahead and just show you what happens. 3:36 And there, it's very quickly. 3:40 My machine's whistling, I'd better turn it off, I'm gonna click stop. 3:41 That was close. 3:46 So, there are going to be many times where you don't know the high end of your range. 3:47 And using infinite streams is the way to go. 3:52 >> All right, now that you've gotten smitten with streams and 3:54 all their amazing aggregate functions, I think we're ready to start 3:58 unparking those items that we've added to our parking lot document. 4:01 And start diving into the fundamentals. 4:04 Now that you've seen how these things work in Java specifically, 4:07 let's take a look at the fundamental concepts in broader strokes. 4:10 This knowledge will prove critical as you design your functional solutions. 4:14 Let's take a quick break. 4:18 And I'd like to make sure you come back to the next stage refreshed. 4:19 You're doing great. 4:22 Go get some fresh air, get your blood pumping, and 4:23 get that brain ready to learn. 4:26 Let's do this. 4:28
You need to sign up for Treehouse in order to download course files.Sign up