1 00:00:00,430 --> 00:00:03,140 So now that you've seen ranges in action, 2 00:00:03,140 --> 00:00:07,910 you might be wondering how you do the step portion of the for loop, right? 3 00:00:07,910 --> 00:00:11,390 So this old school for loop here, that's this i++, right? 4 00:00:11,390 --> 00:00:13,580 So each iteration, what should happen? 5 00:00:13,580 --> 00:00:17,030 Now in a standard for loop, you just increment your index. 6 00:00:17,030 --> 00:00:20,250 But you can do other things, maybe you wanna increment it by 2 or 7 00:00:20,250 --> 00:00:21,290 something like that, right? 8 00:00:21,290 --> 00:00:22,910 So that's called stepping. 9 00:00:22,910 --> 00:00:26,690 So this is a little bit of a stretch but I wanna show you how to do that 10 00:00:26,690 --> 00:00:29,740 declaratively in case you run into that need. 11 00:00:29,740 --> 00:00:32,035 Okay, so let's say, let's see how many companies we have here. 12 00:00:35,000 --> 00:00:37,010 How many companies are hiring? 13 00:00:37,010 --> 00:00:40,330 Over a thousand jobs posted yesterday, let's see. 14 00:00:40,330 --> 00:00:43,532 657 companies, okay. 15 00:00:43,532 --> 00:00:48,490 So, that is too many to display in a menu, obviously, right? 16 00:00:48,490 --> 00:00:52,020 Let's say that we wanna show 20 pages each. 17 00:00:52,020 --> 00:00:54,980 But you wanna choose from each one of those pages, right? 18 00:00:54,980 --> 00:00:58,497 So we wanna show every 20th page, what would be the first company on that page. 19 00:00:58,497 --> 00:01:01,715 And then that go kinda let you choose where in the alphabet to go. 20 00:01:02,940 --> 00:01:05,860 I know it's a bit of a stretch but it's something fun to play with. 21 00:01:05,860 --> 00:01:09,480 So in order to do that, we probably wanna say that our page size, 22 00:01:09,480 --> 00:01:12,560 we'll set a new variable, page size is 20. 23 00:01:12,560 --> 00:01:16,710 And let's calculate how many pages there actually are, right? 24 00:01:16,710 --> 00:01:22,140 So we have companies.size and then we'll divide that by the page size, okay? 25 00:01:22,140 --> 00:01:26,710 And then that way we can change that and this will dynamically fix itself. 26 00:01:26,710 --> 00:01:30,200 Okay, so, there is a method on 27 00:01:30,200 --> 00:01:35,040 IntStreams that will generate an infinite stream of values. 28 00:01:35,040 --> 00:01:36,300 And it's called iterate. 29 00:01:38,380 --> 00:01:42,070 And what iterate does, is it takes a seed or a starting point. 30 00:01:42,070 --> 00:01:45,090 So in this case, we're gonna start at 1, just like we did before. 31 00:01:45,090 --> 00:01:49,340 And then each time it's called upon, it takes the last value, and 32 00:01:49,340 --> 00:01:51,110 runs it through a function. 33 00:01:51,110 --> 00:01:57,140 So that function is gonna take an i, we'll just say, i + pageSize, right? 34 00:01:57,140 --> 00:02:00,420 So each number that this generates the next one. 35 00:02:00,420 --> 00:02:03,440 So the first one will be 1, the second one would be 21. 36 00:02:03,440 --> 00:02:05,372 And the next one would be 41, right? 37 00:02:05,372 --> 00:02:09,250 So it's gonna add 20 each time, that's what we're gonna do. 38 00:02:09,250 --> 00:02:13,450 So we'll use very similar logic to convert our item as we did before. 39 00:02:13,450 --> 00:02:17,673 So we'll say, .mapToObj, and again, that's gonna take an i. 40 00:02:17,673 --> 00:02:22,786 And we'll do String.format. 41 00:02:22,786 --> 00:02:26,219 And we'll give it a d, %s. 42 00:02:26,219 --> 00:02:30,628 And very similar, we'll do i and 43 00:02:30,628 --> 00:02:35,230 we'll do companies.get(i). 44 00:02:35,230 --> 00:02:39,910 Now the reason why we have the page size is to make sure that we can limit this. 45 00:02:39,910 --> 00:02:42,980 Otherwise, it will just keep on running, we need to tell it when to stop. 46 00:02:42,980 --> 00:02:45,364 So we're gonna limit it to the number of pages. 47 00:02:45,364 --> 00:02:47,942 And obviously, if it went past the number of pages, 48 00:02:47,942 --> 00:02:50,880 our companies.get(i) would be out of balance, right? 49 00:02:50,880 --> 00:02:54,150 Because this is not gonna stop, this is not based on any top level. 50 00:02:54,150 --> 00:02:56,680 We didn't specify the end of the range. 51 00:02:56,680 --> 00:03:00,080 We specified an infinite stream. 52 00:03:00,080 --> 00:03:01,390 And then finally, let's print out our 53 00:03:08,693 --> 00:03:11,330 Cool, so let's take a look what this looks like. 54 00:03:13,350 --> 00:03:14,340 Cool, awesome. 55 00:03:16,708 --> 00:03:17,730 So there, right? 56 00:03:17,730 --> 00:03:21,590 1, 21, 41, and that's happening because it's going across here. 57 00:03:21,590 --> 00:03:25,300 So again, I just wanna show you that this is infinite. 58 00:03:25,300 --> 00:03:28,330 And you can actually create infinite loops in functional programming. 59 00:03:28,330 --> 00:03:29,010 So check this out. 60 00:03:29,010 --> 00:03:29,690 I'm gonna do this. 61 00:03:29,690 --> 00:03:32,560 I'm gonna remove the mapToObj because we obviously, 62 00:03:32,560 --> 00:03:33,640 we don't want that to go out of bounds. 63 00:03:33,640 --> 00:03:36,120 And I'm also gonna remove this limit here. 64 00:03:36,120 --> 00:03:38,610 I'm just gonna go ahead and just show you what happens. 65 00:03:40,070 --> 00:03:41,940 And there, it's very quickly. 66 00:03:41,940 --> 00:03:44,460 My machine's whistling, I'd better turn it off, I'm gonna click stop. 67 00:03:46,020 --> 00:03:47,130 That was close. 68 00:03:47,130 --> 00:03:52,020 So, there are going to be many times where you don't know the high end of your range. 69 00:03:52,020 --> 00:03:54,674 And using infinite streams is the way to go. 70 00:03:54,674 --> 00:03:58,030 >> All right, now that you've gotten smitten with streams and 71 00:03:58,030 --> 00:04:01,310 all their amazing aggregate functions, I think we're ready to start 72 00:04:01,310 --> 00:04:04,470 unparking those items that we've added to our parking lot document. 73 00:04:04,470 --> 00:04:06,090 And start diving into the fundamentals. 74 00:04:07,246 --> 00:04:10,580 Now that you've seen how these things work in Java specifically, 75 00:04:10,580 --> 00:04:14,390 let's take a look at the fundamental concepts in broader strokes. 76 00:04:14,390 --> 00:04:18,540 This knowledge will prove critical as you design your functional solutions. 77 00:04:18,540 --> 00:04:19,790 Let's take a quick break. 78 00:04:19,790 --> 00:04:22,840 And I'd like to make sure you come back to the next stage refreshed. 79 00:04:22,840 --> 00:04:23,990 You're doing great. 80 00:04:23,990 --> 00:04:26,310 Go get some fresh air, get your blood pumping, and 81 00:04:26,310 --> 00:04:28,180 get that brain ready to learn. 82 00:04:28,180 --> 00:04:28,680 Let's do this.