Understanding Streams4:56 with Craig Dennis
Streams are a sequence of elements and make use of a functional lazy pipeline.
[SOUND] Welcome back. 0:00 I am so excited to introduce you to our next concept, streams. 0:05 So simply put, a stream is a sequence of elements. 0:10 Now that might sound a whole lot like a collection, but they are indeed different. 0:14 Collections are a data structure that store elements. 0:19 A stream on the other hand carries values from a source through a set of operations. 0:21 And these operations form a pipeline that allow you to express common manipulations 0:27 on those values declaratively. 0:33 There is this magical moment that happens when people are exposed to streams. 0:36 Now some technology gives you this aha moment when you understand its purpose. 0:40 Streams almost always give you this whoa 0:45 response when you first experience their power. 0:49 I'd love to capture your moment, so please make sure you reach out in the community 0:52 or Twitter or email and let me know about your whoa moment. 0:55 Streams are going to require you to think a bit differently. 1:01 But conceptually, it builds upon what we've seen thus far. 1:05 There are three pretty important concepts that I wanna touch on briefly here. 1:08 And then we will explore more in detail as we progress through the stage. 1:12 Streams are lazy. 1:16 They don't do anything until you tell them to. 1:18 So this here is a machine made famous by the inventor Rube Goldberg. 1:20 You might have seen Peewee Herman use one of these to get out of bed and 1:25 feed him some Mister T cereal. 1:28 Or maybe you watched an Okay Go music video. 1:29 This simple one that we built here will do some stuff, right? 1:32 It'll hit some dominoes then finally it will pour this soda into a cup. 1:35 If you'll notice, this is just sitting here. 1:39 It's not doing anything. 1:41 I can add new parts to it that will perform new operations. 1:43 But it's lazy. 1:46 It doesn't do anything until I request it to do so. 1:48 Now these bits in here, these are intermediate operations. 1:52 But this one here, 1:56 the one pouring the soda is the terminal operation, the final straw. 1:57 It's the reason all this happened. 2:02 The intermediate operations got us to determine our operation. 2:04 No point in doing any of the stuff without the terminal operation. 2:08 And this cup here can be fond of as collecting the results. 2:12 Streams work a lot like this, you will create a stream first from some data. 2:16 You add a function to its pipeline through some specific methods that are exposed and 2:21 after you use one of those methods, and append your function. 2:25 The method will return the stream and 2:28 you can have another intermediate function appended to the pipeline. 2:30 The stream doesn't do anything until you tell it to. 2:33 You do this by adding a terminal operation, but 2:37 until you add that operation, your stream will be lazy. 2:40 Just sitting there, like the Rube Goldberg machine. 2:43 Streams are declarative. 2:46 You don't worry about iteration and how things flow down the stream. 2:48 Streams solve problems that you probably have solved in an imperative fashion. 2:53 And just like the declarative method we saw. 2:57 It is much more succinct and legible and 3:00 removes the ceremony that we have become so familiar with. 3:02 They make heavy use of those functional shapes that we've just explored. 3:06 Because streams are declared, streams can run in parallel. 3:10 That is of course if you follow some pretty straightforward 3:14 functional programming rules. 3:17 Here I have five marbles that I'd like to run through this machine. 3:20 Now in order to do this, I have to wait for each one to go through and complete. 3:23 What could I do to make this run faster? 3:28 Hm, what if I had five machines that were exactly like this? 3:32 Couldn't I then just drop a marble in each of these machines? 3:37 That would work, wouldn't it? 3:41 It might take a little bit of effort to set it up. 3:43 But as long as each one of these intermediate operations 3:46 return the exact same thing each time the marble passed through. 3:49 And it didn't require anything outside of the machine itself to work successfully. 3:53 I mean if I made our machine here shoot a dart at a balloon, which was not part of 3:57 the machine and then continued things, it would make the execution different. 4:01 But here, it's [INAUDIBLE]. 4:06 Streams can very easily be made parallel through a simple option. 4:09 It can make five of the exact same stream and run your data through the pipeline. 4:14 And then when they have all completed, combine the results. 4:18 Concurrency or running multiple processes or mini programs can be done imperatively. 4:22 But it is much more challenging than simply flipping a switch. 4:28 There are huge wins here for the declarative syntax and 4:32 again, it only works if you follow some functional programming principles. 4:36 Your functions need to be pure, not causing side effects. 4:40 Now we've got those terms parked, pure and side effects, and 4:44 we'll get some hands-on experience with them here shortly. 4:48 So enough talk, let's get to using these powerful abstractions. 4:51
You need to sign up for Treehouse in order to download course files.Sign up