EP53 – Intro to Streams in Java

43:44
 
แบ่งปัน
 

Manage episode 307556904 series 2398153
โดย Trevor Page และถูกค้นพบโดย Player FM และชุมชนของเรา -- ลิขสิทธิ์นี้เป็นของผู้เผยแพร่ ไม่ใช่ Player FM โดยมีการสตรีมเสียงโดยตรงจากเซิร์ฟเวอร์ผู้เผยแพร่ กดปุ่มติดตามเพื่อติดตามการอัพเดทใน Player FM หรือวาง URL ฟีดนี้ไปยังแอพพอดคาสท์อื่น

GitHub link here: https://github.com/tp02ga/FunWithStreams

In this episode we'll talk about:

  • What are streams and why you should care about them
  • Comparing a simple Stream example to a Looping example
  • Intermediate vs Terminal operations

Episode Transcript

0:09
Welcome to the coders campus podcast, where you'll learn how to code from one of the best teachers in the industry. Whether you're an absolute beginner or a seasoned pro, the coders campus podcast will teach you what you need to know to master the art of programming. And now, your host, Trevor page.

0:28
Oh right, ladies and gentlemen, fellow coders. Welcome back to this next episode of the coders campus podcast, we have hit episode 53. And in this one, as promised, we are going to dive into the topic of streams, which is, I have to say, it's one of my favorite topics. Because of the features that we get to leverage here that again, come from Java eight, we get to, you know, leverage lambdas. And the behaviors that we get out of this code, the things that we can do with streams, the way the code is written, it's just, it's just so much more elegant, it's just so much more beautiful, more readable. Although it takes there's a bit of a learning curve that goes along with it. Once you understand streaming, it's just so wonderful to use. And it, it's a game changer, in my opinion in terms of readability, right and simplicity, right. So but again, all of that is, you know, important to understand that before you can do all that you need to learn this stuff first. So there is a learning curve involved here.

1:38
But my God, guys, it's so much it's so worth it, it's so worth the pain in the struggle to fully understand how to use streams. Because, you know, in my opinion, I never want to go back. So now, obviously, what do I mean by that? What do I mean by going back? And what is a stream? And how, how, why should we care about them? And you know, how do we compare a stream example to something else? And you know, what are the some of the commonly used methods and whatnot that we have with respect to streams, that's sort of what we're gonna be diving into, in today's 53rd episode of this coders campus podcast. So, as always, this podcast is brought to you by V coders campus boot camp. So I'm sure at this point, you've probably heard of it. If not, hey, there's always a first time for everyone. So the coders campus boot camp provides you with all the education that you need to get a job as a coder. And it's not just the education, it's all the copious amounts, the insane amounts of support that goes along with education because education by itself, in my opinion, I know this is a little controversial, is useless. So if you are someone who is fairly new to coding, education alone is in my opinion, useless. Because going from zero, like on a scale of zero to 100, if you're going from zero to like one as a coder, where 100 is job ready, so to speak, and zero is completely not job ready, going from zero to one is so, so hard, right? That's what this this whole podcast is driving towards is just the education alone, it takes so much time and effort to go from like zero to one, as a coder, there's so much so much fundamental stuff that you need to understand and that needs to click, and you probably need to hear it in 10 different ways. And it's just so difficult, right? Unless you are naturally gifted, in which case, it's a lot less difficult. But for the average person, it's very difficult. So that's where this bootcamp helps, right? We take you from we try our best to go from zero to one, I prefer to have someone who already is at one out of 100 with respect to their coding abilities, meaning you already have been exposed to variables and you've already written small console applications. And and maybe you're you've written some methods to take in inputs and give outputs and you understand the basics of methods and variables and whatnot and some data types. Once you're there, once you have all that knowledge, that's where we can take you from one to like 100 Right. In other words, we can get you the job ready in a short period of time in the boot camp by short period of time, I mean six months, okay, that is a short period of time when you know compared with anything else. So that's what the bootcamp is all about. We give you not only the education, but all the support that you need to get from essentially hypothetically speaking, zero to 100 Ideally from one to 100. So, we take you from where you are to being job a job ready coder. So that's what the bootcamp is all about. And that is what allows me to help produce this content. So if you are interested in that if your goal is to get a job as a coder, please check out coderscampus.com/bootcamp. Okay. coderscampus.com/bootcamp that will allow you to learn more about the boot camp, the program itself, what is expected of you in terms of time commitments and whatnot. It is a part time commitment, but it's still a commitment. So if you don't have at least 20 hours a week in your schedule, then don't even think about it. It this is, you know, we have to do a lot of work in six months. So you can check that out coderscampus.com/bootcamp, it is of interest to you feel free to apply. And you can ask all the questions you like, once you've applied, you are able to book a phone call with us. And we can hop on, you know, a phone call or zoom or something. And you can ask all the questions to see, you know, make sure it's a good fit for you and vice versa, make sure that you're a good fit for the bootcamp. So at a 90 96% job placement rate for graduates within six months, this is a very successful program. So for those of my students who graduate yet 96% of them are placed within six months, most are placed within like a month or two, I would say 80% are placed within like a month or two. And that is just because of the the curriculum that we provide, as well as the level of support that you get inside of the boot camp. It is, in my opinion, top class top rated. So we pride ourselves on that education and that support. And we pride ourselves on that 96% outcome number. So like I said, if your goal is to get a job as a coder, check it out coderscampus.com/bootcamp. Okay. Sweet. So let's get into streams, because like I said, my favorite topic, or at least one of my favorite topics. So first, we will dive into what streams are and why you should care about them. So streams were introduced in Java eight, just like lambdas were introduced in Java eight, which we've been talking about in the past few episodes here on the podcast, and

6:55
what are they? Well, they are essentially another way to iterate through collections. Okay, so what that means in plain English is usually you loop through some sort of a data structure that you may have in your typical, you know, workflow when you when you are writing programs when you're writing code. So in Java, a collection is something like a list or a set or a map, or you know, who knows any combination of those or variation of those.

7:28
A collection is just a bunch of objects crammed into one data structure. And typically, you iterate through them. So typically, it's a for loop, where you you loop through each and every one of the items in a list are the items in a set or the items in a map, you iterate through them so that you can perform some sort of action, right? So for example, let's say, you know, you have a typical example I would go to is user login, right. So if you have a list of users, so you got a bunch of users, and you want to, you know, validate that the user credentials that you've been given, in other words, credentials being username and password, you want to iterate through all the users to see if this, this username password matches with what you have in your database, right in your collection, your list of users or something, or maybe a map, you know, where the key value, I don't know, whatever. This is very common, right? Or maybe you're iterating through a list of bank transactions, you want to go through all your recent bank transactions, so that you can, you know, add up the totals or something, you know, this is very common, right, you do it in a for loop it for each loop type thing. So start with the first transaction, start with the first user, go through each and every one and do something as you are iterating. And going through each one of those objects, each one of those elements in the collection. So in the list or the set or the map, or the whatever collection is just another word for a data structure that holds many other objects inside of it. Right? Go back to your episode on data structures, if you don't know what data structure is, if you don't know what a data structure is, yeah, you're definitely maybe too advanced. This episode may be too advanced. But yeah, so that's what we used to do. That was sort of the old way, we would just iterate using a for loop or something. Could be a while loop as well, any loop you would iterate through, right? Well, what streams allows us to do is more or less the same thing. We can instead of you know, doing using a for loop, we can now use a stream to iterate through our collections. Okay, now, a stream is a lot more advanced than a standard for loop. There's a lot of optim optimizations inside of it. There's a lot of, you know, stuff, low level stuff that I don't want to cover in this podcast because in my opinion, you don't really need to know that stuff to be a great coder. Those are all like interview type questions. And again, I talked once before interview questions are so disconnected with the day to day reality of the coding that you need to do. It's so silly To me, and it's so annoying to me, because anyway, that's a whole nother soapbox I can get on. There's a huge disconnect between what is expected of you in an interview and what is expected of you in the real world. And that's really unfortunate. So anyway, the low level inner workings and you know, bits and pieces of how streams work. Those are interview questions, you're not going to need to know that stuff on a day to day basis. So I want to talk about the real world stuff. That's what I focus on in this podcast. So yeah, a stream the way I summarize a stream in plain English, I say it's a fancy for loop. That's essentially what I what I say. It's a fancy way to loop through a collection. Okay, that is the gist of what a stream is. Obviously, there's a lot more to it. But that's the gist. So considering the fact that a stream is just a fancy for loop or a fancy loop in general, why don't we dive into what makes it different, what makes a stream different from an actual loop? Right. So let's go let's go into an example. Now I have a I've created a repository, a GitHub repo that I will link in the show notes. Let me just make a note to do that here. I'll put it near the top of the episode. So GitHub link here, there, I made a note to put that in there. So if you go to coderscampus.com/53, you will be able to go to the show notes quickly, coders coderscampus.com/53. And you'll be able to see the GitHub link to the repos and you'll be able to see the shownotes should take to the repo singular. So you'll be able to see the code that I'm about to explain verbally to you right now. So if you're not driving, and you're able to you can pull up coderscampus.com/53, click on the GitHub link, and you'll be able to see the code that I'm talking about. So let's compare a simple example of an old way to do things meaning using a loop, and the new way to do things meaning using streams. So for our first example, we're just doing a simple filtering. Okay, we just want to filter a bigger list into a subset of that list meaning whittle down the elements in a list to be fewer elements based on some criteria. In this case, to give a concrete concrete example, I want to talk about a list of names. So we have a bunch of names in a list. A name starting with a going all the way to j. So we have Andrew Betty, Charlie, Darla Eddie Franca, George Hannah, Ivan Juliet. So what does that 10 names ABCDE, F, G, H, I, J. Yeah, 10 names. Look at that. I magically landed on 10. Nice, round number. So yeah, we have all those names in a list. Again, it doesn't matter what the names are, it doesn't matter that they're ABCDEFG. That's just I need to generate some names. And I just did it that way. So the content here doesn't really matter. Other than it's a, it's a bunch of strings. Okay. In this case, it's names because that's more meaningful. So you want to filter this list of names. Okay, this is something that you might want to do in the real world. Here's a bunch of data. And we want to filter that data based on some criteria. In this case, we have a list of names, and I want to filter it based on names that contain a R, T, okay, I don't know why I'd want to do that. That's just a random example that I came up with and chose to put in here. But I want to filter my list by names that contain the string AR, together. Okay, so, given Andrew Betty, Charlie, Darla Eddie Franca, George, Hannah, Ivan, Juliette, can you imagine which of those names contain the combination of letters AR together? Probably not off the top of your head, you might be able to pick one up. But anyway, that's why filtering using you know, code is helpful. So we want to filter the names by names that contain AR. So how do you do that the old way? How do you filter names by a given, you know, given parameter or given criteria? Well, the way you would do it is you would start with the filtered list, you would instantiate it as an array list. In other words, you'd have an empty list, I called it filtered list. And this empty, empty list, specifically ArrayList is waiting to be populated with the matching subset of data here that we're working with that we want to filter down towards, right. So we have we start with an empty list, we pass in our full list the names we pass in the parameter that we want to use. In this case, we want to filter by AR. And then we have our initialized filtered list or initialized empty list. And we want to start iterating through the full list of names. Right, so the full list of names, other words, all 10 names, you want to start iterating through them each one at a time. How do you do that? Well, we use a for loop before each loop to be specific. So we use a string name in names. So as you go through each of the names, you assign it to the variable name. And then we have a variable name in our for loop that we can then use, right, so name is just a string. So then inside of our for loop, we use that name variable. And we say name dot contains are, you know, filter parameters are criteria, because named contains is sort of the the filter. In this case, again, it doesn't necessarily matter what the filter is, and what the parameters are to that are the criteria are.

15:33
What just matters is how do we accomplish this task, right, so we start with an empty list, we have to do a for loop to iterate through all of our collection. In other words, we're iterating through all of our names. And then we say, hey, if this particular name contains our parameters, in other words, contains, you know, ar, is the filter that we're using, in this case, then cool, we have a match this name contains AR, so added to our filtered list. So our filtered list was the array list that was empty at the beginning, we want to populate this new empty list with every matching name that matches the AR, you know, filter. So we add that to the filtered list. And then at the end, once we get through all of our names in the for loop, at the end, we should have a filter to list a subset of the full list that contains our matching names, right? And then we want to return that. So that's it. I mean, that's how you do that with a for loop. Okay, start with an empty list. For each name, find out when the name contains the filter parameter, add it to the filter list. And then at the end, you return that filtered list, which will be a subset of the full list. Okay, so it makes sense. That's, that's fairly straightforward. It's 1234, you know, 567 ish lines of code. Not too bad, not too crazy. So how do we do this in the quote unquote, new way? How do we do this with the streams? Right? Let's talk about it. So in my code, I have a class called old way and new way. So these are two classes, again, not the greatest names for classes. But the old way is what I was just talking about, it was doing this filtering, there was a method in there, I omitted the fact that there was a method doing all this, but it was a method called the filter results. That took two inputs, all the names and the filter parameter. So this same code exists in the new way, class file. So the new the old way, the new way, have the same method that we're invoking here to return the filtered list of names. So we have that same method here, but in a new way, we do not do a for loop. Okay, we're using a stream. So how do you stream things? Well, the stream is based off of the collection. So all pretty much all of the Java, the existing Java collections, have a way to stream, okay, with a list. In this case, we're using a list. The list interface has a dot stream method now. So you're able to do names dot stream. So that starts the process of streaming. And again, what is a stream a stream is just another way to loop. Okay, as we did before, we created a for loop, right? In this case, we don't have to do the for loop, because that's kind of like what the stream is doing. It's allowing us to iterate so to speak. Okay, cool. So we do names dot stream. But then, well, what do we do? Now? Remember, we want to filter these results. How do you do like an if statement to see if a name matches another, you know, matches or contains this filter parameter or whatever, right? So the way streams works, is when you do dot stream, it returns that method returns a stream type.

18:58
And that type has a generic type that it accepts type T or whatever, right? So we have a stream of type t, but what is type T here? We have a stream of something. So what are we streaming? Well, remember, we have a list a collection of what we're dealing with names right now, it's a collection of what it's a collection of string data types, right? That is what our, our collection is, it's a it's a list of string. So when we're doing names dot stream, we're streaming what we are streaming string type. So that that of the keyword there that I use list of string, the of is what's important here. That's what I use in my brain. If the collection it's a collection of something, so that's what the stream is, is iterating over right? It's a stream of string. Unfortunately, those two words are very similar sounding does translate well with audio. So list of string, what's the same thing, same thing. List of string it now it is a stream of string objects. Okay, so we're streaming strings. So when we say names dot stream, which is the new method, dot stream is the fancy method that starts this whole streaming, it returns a stream of strings. Okay, now what can we do with that data type stream of whatever stream of type t, there's a whole bunch of stuff that we can do. So if you do if you do a.on, that stream, there's a whole bunch of other methods that you can do on a stream of whatever type. Okay, well, what can we do? Well, guess what, when you do that on your screen on your screen, and you hit the dot, after the stream method, because getting stream returns a stream of whatever T type, right? When you do a.it shows you all the other methods that you can do on a stream of something. And there are quite a few. There's, there's a bunch of beautiful things that we can do. The one that we want to focus on is we want to focus on the filter. And guess what? Filter takes a predicate? Hey, do you remember what the predicate is? We talked about that in the past episode, I think the last episode, so on 52, we talked about what a predicate is, a predicate takes a generic type t, and returns what it returns a Boolean. Okay, so, predicate takes type t, returns a Boolean, what type T are we talking about here? Well guess what the type is String, because we're streaming strings. So this type here, the the generic type that we're working with, sort of gets filtered through with each one of these steps that we want to do each one of these methods that we are changing, because what we're doing here is we're chaining, we're chaining methods, dot stream, dot filter, is what we're going to be doing dot stream dot filter, so we can chain methods on top of each other. If you want to, you can assign dot stream to a new variable, and then take that new variable name and then on that new variable name, do dot filter. But that's just an extra bunch of noise that you're adding in there. That's, that's okay, if you want to debug in terms of like outputting stuff to the console, or to the system out print line, or whatever. If you want to do it step by step and really have a strong understanding of each one of these steps and have it be very verbose, which defeats the purpose of using streams. But if you want it to be very verbose, so that you can understand it, by all means, go for it. But really the way streams are meant to be used and consumed is using this method chaining concept. So dot stream dot filter, and dot filter is a method that takes a predicate. So the predicate takes type t, remember, it takes type T and returns Boolean, the type in this case is going to be a string. So the filter takes a string as an input in this case, and returns a Boolean. So what is filter do? filter will return true it sorry, I should say IF filter returns true on a stream, that it keeps that particular

23:25
item that you're streaming through, it keeps that particular item in the stream, so to speak, downstream, if you will, it keeps that particular item. And you can do more with it. If If the filter returns false, it throws away that item, it does not care about it. It does not pass it down to the next step. Right. So filtering allows us to keep the things we want and throw away the things that we don't want, which is essentially what we were doing the old way, which was to say, hey, you know, for loop a for each loop, if the name matches our criteria, then add it to a new list. Right? So that's sort of what we're doing here. We're not necessarily Well, we can add it to a new list. But anyway, we're we're keeping going we're considering we're moving forward with anything that returns true here with this predicate with this filter predicate. Okay, so stream dot filter, and then how to use a predicate. Remember, it's just a functional interface that takes in that one, it's one method, I think the film predicates, I forget what the method is called. But it doesn't even matter. You don't need to know the name of the method. Because you can just use the lambda syntax here. So remember our lambda syntax arrows in the middle, to the left of the arrow is the input parameter name, and to the right is the body where we can return something. So dot filter takes the predicate dot filter, we're going to put in name as the name of our variable on the left hand side of the arrow function or the the arrow lambda syntax, because that's essentially what we're filtering here we're filtering a name, because it's a list of names. So really, the way my brain works is whatever we're streaming, whatever we are iterating, over, probably has an S on, it's probably a pluralized, the version of whatever. So names is the plural name, plural variable name here. So names has the s, well, what are we streaming, we're streaming the individual name. So you just cut the s off of the variable name. That's sort of how I typically set up. Streaming makes it very simple and straightforward and easy to understand and read. So we're iterating. we're iterating over names of via a stream. So again, I'm just I keep saying the same code over and over again, by the way, this code is three lines that I keep talking about here with this new way, it's three lines of code, as opposed to like seven before, so it's much more succinct, to get names dot stream,

25:59
dot filter. And then filter is a method that takes in a lambda A predicate. So to do that, we have name on the left hand side of the of the arrow. And then what is on the right hand side, well, remember, the right hand side has to return a boolean value. So what is this boolean value body function, whatever that we need to create here? Well, it's the same as we did in the old way, we just said if name dot contains the parameter, the criteria then return true, right, so we can just do that here, the short form, we don't even need to add body, we don't have to add, sorry, the curly brackets, we can just say, name, because that's the variable name that we chose to name it on the left hand side of the lambda, we can say on the right hand side name dot contains the filter parameter, right, the criteria that we're passing in, and that's it. Right, that's name is on the left, and then name contains the filter parameter on the right. Because named contains will return true if the name contains that filter parameter, right? In this case, AR is a string that we're filtering by. Cool. So we did names dot stream, which returned a string stream of string, then we did dot filter. And we put in our filter parameters, well, what is filter return filter returns another stream of string. Okay, same thing, but the difference here is, it's going to return a subset from the stream above, because we're filtering out some stuff. So if the name contains a our, the filter will return it as a stream of strings in this case, but for our example, it's actually gonna be two names. So I should probably have said that before. So the two names that is going to return here are Charlie and Darla Charlie contains contains AR, ch, ar, right for Charlie. So AR is in there. So boom, we have a match. So it's going to return that in a stream of strings. And it's also going to return Darla, because Darla has D A AR AR, right, we're always looking for the AR is in the names. So we're going to below the filter when we chain another method onto dot filter, because that's what this whole thing can do. We can chain methods onto each other filter returns a stream of strings, but in this case, it's only two string strings, right. It's only it's a stream of two strings, because to match the filter criteria, Charlie and Darla, whereas before and the step above, if we say names, dot stream, names dot stream, we'll have all 10 names. Because we haven't filtered anything, this is just, I want to stream all of the names, right. So names dot stream, will be a stream of 10 strings, right 10 names, but then dot filter will return a stream of strings, but it's only going to be two string strings in that stream. Sorry, again, stream string stream strings. I'm trying to enunciate as best as I can. So yeah, dot filter will give us a subset, right? They'll give us two strings in a single stream. Cool. So now what do we do? Well, we can do another.in. This case, we're doing something called dot collect. And we're saying collectors.to list. I went over that really quickly. Because there's there's more on your talk about about dot collect. So where the heck did that come from? Trevor, you probably are saying I was with you with a dot stream that sort of makes sense. You know, you're iterating over all of the, you know, items in the collection. we're iterating all over all 10 names. And then you said dot filter, and that got a bit more confusing. We're filtering, but you know, true means keep it false means throw it away and send it downstream. Okay, I think I understand what you mean. But then what the heck does collect mean so before I dive into what I collected means you need to understand that when you're streaming, there are, I think a couple of maybe more than a couple. But there are a couple types of methods that we can that we can use here. There, I should say couple of categories of methods when streaming. So there are a couple of categories, or at least a couple of important categories of methods. When we're talking about streams, the first category is

30:31
called an intermediate operation. So filter is an intermediate operation, you can categorize, filter as an intermediate operation. Okay? What that means is, you can perform a bunch of different intermediate operations on a stream, if you like, you can perform one you can perform none or you can perform many intermediate operations on a stream. In other words, these intermediate operations allow you to mess with the stream, change it in some way, or, or shape or form, right. In this case, we're filtering so we are removing elements from our stream. There are other intermediate operations that you can do that modify the stream. We'll talk about another one in well, can we talk about it in this episode, How long, I've been talking for 30 minutes. Okay, so maybe we'll talk about that in the next episode, another oper, another intermediate operation that you can do. So you start a stream, that's like the start of the whole thing, you can perform zero or sort of, I don't wanna say Unlimited, but zero or more intermediate operations on the stream, and then you can perform one terminal operation. Okay, a terminal operation is an operation that you can do once at the very end. Okay, so typically, the way a stream goes is the stream starts. And then on the stream, you do zero or more intermediary, intermediate operations, and then you perform one terminal operation. That is the generic and common blueprint for streams. Okay, so a terminal operation is what you do at the end. And the terminal operations typically take in some sort of, I think it's a supplier is the is that right supplier, does a supplier return? I can't remember anymore. We just talked about this, I think a supplier just returned something, as it takes something in, it returns a result. I forget if it takes, if it takes no, it doesn't take a parameter. At least I don't think it has to, because it already knows from above what what it has access to. So I believe the supplier just returns something back. So in this case, dot collect, has picked a supplier and the supplier in this case is called, we use something called collector.to list, or sorry, collectors with an S collectors is a special sort of helper method that allows us to do a bunch of stuff. So collector.to list allows us to take in the items that are coming from upstream, and then just says, Take everything that we have at this point and turn it into a list. Okay, so I know that was a bit confusing, I didn't do a great job explaining it. I got mixed up there with supplier versus consumer. Right, those are the two I just talked about this, you can see you can hopefully take some

33:59
some solace take feel good about the fact that I'm even still confused about this stuff, right? So and I've been doing this for like, I don't know, how long but using Java eight stuff, I don't know years, it's been two years that I've been doing this. So it just goes to show you don't need to know I'm trying to get into the inner workings of how everything and you don't need to know the inner workings all i All I think about and all I know is that I can do dot collect, which is a terminal operation which you need to have a terminal operation with a stream to do anything with it, do anything useful with it. So dot collect is a terminal operation and it allows us to take what items we have left in the stream and then turn them into something useful like a list or a set or a map or whatever, right. So there's a bunch of things that you can do with a collector. So you can say collect dot collect as the as the method. So dot collector is the method which is called a terminal operation. And then you pass in collectors dot something. So collectors.to list will turn whatever items we have left into a list. Okay, so that's it. That was a lot of explaining. But really all that explaining was for three lines of code, right? Names dot stream stream is a method for invoking it. So we have round brackets, names dot stream with round brackets, dot filter. With round brackets, what are we putting in the filter, we're putting a lambda name on the left arrow named contains filter params. On the right, that's how we filter out all the names that don't have AR in them. And then chaining another method dot collect, get, it's a method we're invoking. So we have to the open close parentheses here. And then we pass in collectors.to. List, collectors is the utility type object that we use here. That allows us to yeah, do all sorts of different ways to collect stuff, in this case, collectors.to list takes whatever we have upstream and turns it into a list. So then we can return that. So that's what we do to return a list of names that are filtered by a certain result. That's how we do it, we stream we filter, we collect done three lines of code. Cool. So that's what streams are, they are a different way to do a loop. And streams are composed of typically the initial dot stream to get started, then it has zero or more intermediary options. In this case, one of the intermediary options or operations that we did was filter, which reduces the number of items because we're filtering out stuff. And then we collect the terminal operation. So stream it, zero or more intermediary operations. And then collect is the terminal operation. So a terminal operation. So another example of a terminal operations do a for each. So which is you know, instead of saying collect, I could have said dot for each. And the difference there is dot collect allows us to put all that stuff into a variable, right? In this case, we're putting it all into a list. So we're taking the filtered results and putting it into a list, which is cool, because you want we want to return that list, right? We could have also alternatively use dot for each. So instead of just returning those last two names that we filtered, instead of just having those two names and returning them to a new list, we could have just said dot for each and done something with them, right? That's another terminal operation. In this case, we want to be able to return that list so that we can do something with it. But hey, if all we needed to do was like print out those names, we could have done done a dot for each and just printed them out. Right, that would have been another way to do it. So apologies for being long winded here. But I guess that's the whole point of this podcast is to be long winded. My hope was I could talk about another intermediate intermediary step. But yeah, we will all let your brain digest everything that we just talked about about streams. And in the next episode, we will dive into another really cool intermediary option called Map, which is probably the single most confused, that's right word confusing, intermediary operation that exists, because it shares the name of the data structure, right? In Java, there's a data structure called Map. And whenever my students learn about streams, and they hear about dot map, they immediately think it means convert it to a hash map. And it's like no, it does not mean that. So we will dive into the complexities of what dot map is as an intermediary operation. And obviously, we'll dive into some more stuff probably in the next

38:48
episode of the podcast, because there's more obviously to talk about with respect to streams. But to sum up, streams are less verbose way to do operations on a collection of objects. If I zoomed out to the 10,000 foot view, or whatever, 50,000 foot view, that is the gist of what streams do, because in our daily lives as coders, we do crud, all day long. crud, creating, reading, updating, deleting data, create, read, update, delete, see our UD crud, right, that's what we do on a day to day basis, we do crud, and as a part of crud, with reading data with the R step. Typically, we read data from the database. And when we read the data from a database, or a file or whatever your you know, your what's the what's the, your persistence thing, if it's a database, if it's a file, if it's a, an API, whatever your you know, data stream coming in is, typically we read it into a collection of some sort, and then typically We take that collection, and then we do something with it, we modify it, we enhance it, we tweak it, we do something with that collection that we've read from a database or an API or a file or what have you. We do something with that data, we modify it, and then we send it to the front end. And vice versa, we take it from the front end, we modify it, and we send it to the backend, right? That intermediary step there not to use the same word that exists in streams, that middle step that we do with respect to either pulling it from the database and putting it the front end, or taking it from the front end and putting it in the database. There's that middle step, where we typically iterate over a collection of items and do something with it, right. That's where streams shine. And there's obviously more streams and what we've talked about. But this is where this is something we do all the time, we see a lot of for loops, iterating over things and modifying things. And what happens is the statements, these blocks of code of for loops, can get really long and verbose. And really be difficult to understand. And you'll be amazed at how much more easily you can understand code when it is not in a for loop. And rather when it is inside of a stream. That is where streams shine. They are so much more readable. You can look at it and understand a more complex stream of operations in seconds when compared to probably upwards of hours with a for loop, maybe not hours, maybe that's a bit, too, you know, going too far. But minutes, I would say seconds versus minutes when it comes to understanding code and reading it. So that's the beauty of streams. And having said that, like I said at the top of this episode, the boot camp is something that I would highly recommend you checking out if you haven't already done so coderscampus.com/bootcamp allows you to Yeah, dip your toes in those waters, check out with the boot campers about learn more about it, see if it'd be a good fit for you. Like I said, if your goal is to get a job as a coder, oh my goodness, this is what I shine that and this is what I've dedicated my now career two is helping people transition from where they are to being job ready in as short a period as possible with as least amount of pain as possible. That's the point of the bootcamp. In my opinion. Like I said before, it is not only the fastest way to do it, it is also the cheapest way to do it. And there's data to back that up when it comes to completely self taught versus doing something like a boot camp versus doing something like a college degree. So it is the sweet spot in my opinion with respect to achieving the goal of getting a job in the real world. So if you are at you know, one out of 100 in terms of job readiness, I'd highly recommend applying to the boot camp coderscampus.com/bootcamp. I will see you there. Hopefully I will talk to you soon. Otherwise, I will see you in the next episode of the podcast. Take care everyone. Happy learning and bye for now.

42:58
Thanks for listening to this episode of the coders campus podcast. But before you go, Trevor has a favorite ask you. In order to keep these episodes free, he'd love for you to leave a rating and review the podcast on iTunes. Just go to coders campus.com/review to leave your own rating and review of the show. So if you have 30 seconds to spare right now, please help out by leaving a rating and review via coders campus.com/review It will ensure that you continue to get these awesome free podcast episodes each and every week. So if you like free swag, head on over to coders campus.com/review Happy Learning

The post EP53 – Intro to Streams in Java appeared first on Coders Campus.

123 ตอน