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

In this episode we'll talk about IntStreams and why they're useful in Java.

IntStreams can be used to replace a common looping mechanism as well as provide a very helpful utility for gathering analytics on data in a non-verbose way.

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
All right, ladies and gentlemen, fellow coders. Welcome back to this next lesson in our series in the podcast where we are diving into streams in Java. So thanks so much for joining, as always, always a pleasure to be on this side of the microphone, and hoping to deliver lots and lots of useful tidbits to you. So today, we're talking about into streams, tricks and analytics within streams was the title I came up with, because it sounded neat, it rhymes. So deal with it. So anyway, what we're gonna be covering is essentially, what is an in stream? How is it different from a regular stream in Java? When would we use it? What are some, you know, nifty little tricks that we can use it for. And as well as you know, getting into more data analytic stuff, which is something that, you know, Python gets a lot of a lot of notoriety for, using, you know, data science and whatnot. But the, hey, there's some tools in Java for data science as well. And I'll be it this one is very simplistic, but helpful. I've used it before for, you know, analyzing data and giving reports and whatnot. So yeah, without further ado, let's dive into this topic, while I pull up my example. So as always, if you're following along with the examples that I'm posting, you can do so via coderscampus.com/56, that should redirect you to a little blog post where I will include the GitHub repository that I'm using, within within that GitHub repository, there should be a package called com.coderscampus.intStream. And there's a couple of files in there that you can look at that will, you know, represent the code that I'm going to be talking to you about right now. So yeah, you don't have to go there. Now. It's okay, if you're driving or something or doing dishes and or working out, you could just listen. But just remember, this code is available, if you want to go back and look at it for reference on your computer, to coderscampus.com/56. So basically, what is an entire stream, we'll start there. So in streams are a variation of a stream in Java. But more specifically, it is dealing with, as it says, In the name integers. So it allows you to do more specific operations, you know, chaining methods and whatnot, like we're used to doing with streams where you, you know, you do a dot filter, dot this dot that or whatever, with an industry that allows you to do it unlocks a couple of specific, you know, dot, essentially specific methods, specific functions that you can call that you can invoke that do some helpful things. So in in stream is just, hey, we're dealing with integers here. So the only valid object that you can be streaming over the only valid, you know, collection of things that you can be streaming over is an integer or an integer set of integers. Or I suppose I should say, a collection of integers to be more specific. So, yeah, if you're not using integers here, if you're not streaming over integers, then you can, you cannot use an int stream. Okay, that's the first thing to understand this int stream is just for integers. Streams are for any objects. So long as they're not, you know, primitives. Well, I guess I shouldn't say any objects. Streams are good for any collections. But into streams are specifically for integers. Okay. So having said that, the example that I have here is using sales data. So it's something very simple. There is no you know, this is how we do it the old way. And this is how we do it the new way. The old way of doing this is awful. If you had to code this out yourself, it would be tons of lines of code. The finished product here in front of me is is like what 24678, you know, about eight lines of code versus it would be, I don't know 50 or more, I don't know, off top my head. It would just be a lot of work to do this all yourself.

4:43
So yeah, we boil it down to eight lines of code here. But basically what we're doing here is we're looking at sales data. This could be any data, I just created a sales data object class, more specifically, and it's just a plain old Java object inside of it is just two properties. Restoring one is the date, the date of sales, and the other is the amount. So the number of sales made on a given date. Now it's an integer amount, it's not a it's not $1 amount. So it's not like a double or a big, you know, into big, double big. What does it anyway? Big decimal, it's not a big decimal or a double type. It's just integer. So this represents some hypothetical number of individual units sold on a given date. Okay, that's really what we're what we are working with here. But it's simple. It's a simple it has a date and an amount as an integer. The date is a local date type. So we're using Java, what is this Java eight? Yeah, Java eight type, sorry, my phone is making notification messages. If that was if you have an Android phone, and you're looking at your phone right now, that was my phone.

5:52
Okay, so just a date and an amount was get with getters and setters, that is what is inside of the sales data class. Very, very simple, just storing two things. So having said that, I also have a little method that I created to just populate a list of sales data. So I'm just populating an arbitrary number of sales data objects and putting them into a list. In my code here, I just have it generating 100. So all this is doing is randomly generating integers and randomly generating dates, popping them into new sales data, you know, objects, and throwing those into a list. It's just a list of sales data that has data inside of it. Now, getting into using an entire stream, we can start streaming over our data, which we just do the normal way we always stream which is we say sales data dot stream. Okay, so nothing, nothing earth shattering, here, we're just doing a regular old stream. So what this will return to us, when we do the sales data dot stream, is it is going to return a stream a normal stream of sales data objects, okay. But what we can do here is we want to turn this into an int stream, because we want to do some statistical analysis on the data. Okay, specifically on the int data, the integer data, so what we can do is we can do for a regular stream, you can perform a dot map to int, map to int. So we've talked about map before a couple of lessons or episodes ago, map is just where we take, you know, something coming upstream. So like, you know, like an object or whatever. And we are, you know, sort of narrowing down to a subset of properties for that, you know, those collection of objects that we're iterating over, okay, that we're streaming over. So, this allows you to narrow into, essentially a different it maps it to a different data type. Normally, we use it to narrow down to a specific property of the objects that we're streaming over. In other words, for this one, we want to narrow it down to just the amount the sales data amounts, because remember, I said the amount was stored as integer, the date in sales data was stored as a local date. So again, sales data has a date, and amount. But what we want to do is you want to narrow down to just the amount. Okay, so that's sort of what map can do map can narrow down to individual properties, or can can completely transform into something, you know, entirely different. It's up to you and how you want to use map. But that's map, we've already talked about map, I want to talk about map to int. So it does the exact same thing. The only difference is what the dot map, how dot map Dipper differs, not dippers, difference from dot map to int, is that map returns a stream of the objects again, and a while stream of the output of whatever it is that you're transport, mapping it to the map to int returns an int stream. Okay, it's not an int stream of anything, because it's we already know it's of integers, right? So it's not an int stream of something, it just returns a plain old in stream because we already know it's a stream of integers. So anyway, that's what we're dealing with here. So map to int, translates transforms the data coming upstream into an int stream. But then we need more data here. We can't just say map to into the magically it'll work. You need to tell it what property from the objects that we're streaming over. What property is the property that we want to use this industry for? How do we want to stream over these integers? So when you tell it what property we're streaming over here, data the individual property? So as I've alluded to before, the property that we want to stream over is the amount property because for sale data, the amount is an integer. Okay, so we can say dot map to int when we're streaming. And we pass in the, the get amount is the the getter method that exists for sales data. So you pass in get amount. And that is what we can use to stream to create our int stream from. Okay, so we pass in the get amount. So this you want to pass and get amount, you can either use the normal lambda function syntax where, you know, the left hand side is because it's the getter method, it will take nothing, I believe, I have it written out the other way. I'm using the double colon syntax. Let me just see if it works. If I use the double, double round brackets.

10:46
Oh, no, I need to pass it in, right, you need to pass in the I shouldn't say double round brackets, you're passing in the sales data from above. So you can call it S or something. And then you can say s dot get amount, right. So when you say dot map to enter, you're passing in a lambda function, it takes the input of the object that's being iterated over being streamed over, which is the sales data, you can call it whatever you like, called call it S. And then you can say s dot get amount on the other side of the arrow function. Alternatively, you can just say, sales data, the, the class name, so sales day with a capital S, double colon, is used to give you access to the methods, right, so you can say double colon get amount, right. So that way, you don't have to have the lambda function, you just sort of pass the pass the function. And it's another way to do the same thing. They both do the same thing. So in my example, I just use that double colon format. So again, sales data dot stream, dot map to int, and pass in the sales data's get amount function. Cool. So now what do we have downstream? What could what sort of dot operations can we do now that we've turned it into an entire stream? There's tons of there's, there's a tons of stuff, there's tons of stuff. One of which is called summary statistics. But I'll get there in a moment, you can do on an in stream, because we know we are streaming over integers, you can do a dot sum, because we simply know we're in there's integers here, we know we can sum up the integers, you can do dot sorted callback to the last episode, it will just know how to sort and insert an int stream because it's just integers. We can what else we can do a max or a min on it, which returns optional int, because Optionally, you know, what if there's not a maximum in there, what if it's empty, or I don't know, maybe there's two that are the same thing. Anyway, you can do max and the min, you can limit the number of, of entries you want to stream over if for some reason that is of use to you, maybe you can sort it and then limit to like, I don't know, the first 10s. Anyway, you can, you can do a bunch of stuff, you can count, you can get an average, you can also convert it to a double stream or a long stream. Okay, because there's not just an industry, there's also double stream and long stream. And pretty much all the stuff we're talking about with an inter stream applies to double stream and long stream as well. Okay. So having said that, there's tons of stuff you can do with an in stream that is specific to iterating over integers. And that's where my mind when I first learned about in streams, it just, it was too much. When I was learning about streams, I was like, well, to stream, there's in stream and double stream as long stream like, what are all these types. And these are all confusing. So don't be too overwhelmed or confused by these types that we're using here is just a stream of integers. And all the only reason why we have a different type there is so that we can unlock these additional functions that I just spoke about that apply specifically to integers that we know we can apply to integers. Okay, because if you're streaming over users, you can't do like stream over users and then say dot average, give me the average user. What the heck does that mean, right? There is no such thing as an average user. Like, you need to define that. And it doesn't make sense. Or give me the maximum user. It's like, what, what does that mean? Give me the maximum and minimum users? I don't know. That doesn't make sense. Conceptually speaking, you need to give me more context. I'm missing context. But if you say give me the maximum integer, and the minimum integer, right, or the average integer, that makes sense, you know how to how to find the maximum integer in a given list of integers, or the minimum integer in a given list of integers or the average value of all the list of integers, right? Those conceptually make sense. So that's what an int stream unlocks. Because we are iterating over a stream of integers. We can do all these extra things. Now, I want to dive into what I spoke about the very beginning there of the all the different functions that we can use on in stream and that was something called dot summer. Statistics. So what does dot summary statistics do? It returns another type called int, summary, statistics. What the heck is an int summary statistics type? Well, all this type is is just a class that allows you to get access to the Common Data Analytics. Remember, I promised I was gonna talk about analytics today, tricks and analytics, these this event summary statistics type, or class gives you access to the average account, the max, the min and the sum all in one place.

15:39
Okay, it gives you access to the average account, the max, the min and the sum. So it gives you access to five sort of aggregation type data, things, great for analytics, right? Great for reports and whatnot. So in three lines of code here, although it took me a long time to explain the three lines of code, in three lines of code, we can now get access to this event summary statistics object and get the count of the data, the max of the data, the min of the data, the average of data and the sum of the data. In other words, give me a count of all the sales that happened, give me the maximum sales, give me the minimum sales number, give me the average sales number, and give me the sum of all the sales figures together. Okay, so you can get all five of those things and print it out to a report. Cool. So that's sort of what inter stream allows you to do from a statistics or a analytics data standpoint, right? It allows you to do all that stuff, which is nice. It's very convenient. And like I said, to do all that statistics yourself to do all those number crunching things and processes and looping and if statements and, you know, switching out the maxes and the mins and trying to calculate a sum and get an average and account like doing all that it just is a lot of work. But we can boil it down to three lines of code now, stream it, map it to an int, get the summary statistics done. Cool. So that is the analytics or statistics point up or part of this conversation. The other part I said is you can do a trick, there's a trick that you can do with an industry that might, you know, impress your mom, or your friends. And that is you can use an input stream to generate, essentially a range of numbers that you can then iterate over. In other words, this is another way to do a for loop. Because you needed another way to do a for loop. You know, you can do a for loop with the old, you know, boilerplate way, which is, you know, for Indi equals zero, i is less than whatever i plus plus, there's that syntax for a for loop. There's also a for each loop, where you can do, you know, for, and then you have the data type, and then the individual object and the colon, and then you do the collection of objects. There's that concept of the for each loop. But then there was this idea of streams, regular streams, where you can do a dot stream, and then you can do a dot for each as well. Right, so you can do a dot for each but and that's fine. That's that's another way to do a for loop over a collection of objects as a stream. Cool, nothing wrong with that. Very helpful. But what if you want to do a different range, but you still want to do that same syntax, you want to have a range from instead of maybe doing all of them, you want to do from zero to 10, specifically, or zero to some number minus 10, I don't know whatever the whatever your little heart desires, you can use int stream to do that. So you can generate a range using int stream dot range, okay, into stream dot range allows you to have or gives you two inputs, so you have dot range, and it takes two inputs, the inclusive integer and the exclusive integer. Okay. In other words, this is from like zero to 10. You know, if you want to go from zero to 10. In other words, you want to iterate 10 times you go from zero to 10, exclusive, or zero is inclusive, 10 is exclusive, so you really only go from zero all the way to nine, and that you stop. That's what the int stream dot range will do for you. So you can do zero to whatever, there's also another one. Instead of range, it's called range closed. What that does is it allows you to do the instead of the end being exclusive, the end is inclusive, right? So instead of you want instead of wanting or having to do zero to nine, now you can have a choice to do one to 10, right, it's again, you could always do this with any other for loop structure. But it's just it gives you you know, you can do it with an in stream. So you'll see this in my code. I do Just to show off that I, you know that I know how to do this, you can do in stream dot range, you can see that the code when I'm generating my sales data with all the random numbers and stuff, I use an input stream to do that range. And then I do all the stuff to,

20:14
you know, go through, and you know, from zero to whatever, completely unnecessary. It's entirely unnecessary. But it's, it's just another way to do a for loop. In this case, we, I don't have a collection of objects to iterate over. All I'm given is as the input is just hey, the number of sales, you know, number of the total number of sales objects that I want our sales data objects that I want to create. That's the input to this generate sales data method that I'm talking about on my screen right now that you guys can't see. But the function that I use to generate sales data, the only input that it has is an is an integer, which is the number of sales. So you can't do like a stream, per se, on that you can't do a normal stream on that. Because it's not a collection, you can stream on a collection. But in this case, we don't have a collection. So we would have to write out a regular old for loop a for it, i equals zero, where i is less than number of sales, i plus plus. So because I would have been forced to write that instead, I'm using in stream dot range, zero comma number of sales. So that just allows me to save myself from typing out the boilerplate for for loop right. Now, having said that, do you always want to do that? I don't know, I don't think there's a huge reason to do this. Other than, like I said, it's, you know, it's potentially more readable. It's, it's using or utilizing the functional programming paradigm. So you do int stream dot range, and that dot range will return an int stream, which I then use dot boxed on. Because what dot box does is it returns a stream consisting of the elements of the int stream, each boxed into an integer. Okay, so what that means is, is we're just converting back to a normal stream, instead of an in stream, we're converting back to a normal stream. And each of the elements now that we're iterating over, are available as integers. That's all. So we're taking primitive INTZ, and boxing them into integer objects, and iterating over them as a collection. So if you have a range of zero to 100, or something, you will get a stream of numbers from zero all the way to 99. And then you can do whatever you want with those, right? So what I did was I just did a dot map, and I mapped the integer to something completely different. In other words, I completely ignore the integer, the I in this case, I completely ignored it and just mapped it to generate a new sales data object and they collected all those new sales data objects, 100 of them into a list and return that list. Okay, so that's how I generated my sales data. It wasn't, you know, again, I could have done all this in a for loop. But when you use an event stream, you get to use, you know, dot map and dot collect. And now this dot box thing, which just translates from an in stream back to a regular old stream. Cool. So, yeah, that is the gist of an event stream. And again, you can more or less copy, paste, everything I've talked about here, into a double stream or a long stream. Now, there's probably some operations that are specific to doubles, or specific long's that are, you know, useful and whatnot. So those will be available there. But that's a simple Google search. You don't need me to go over every single one of those individually. I'm sure you are fully capable of looking up what those types of streams are and what makes them different from an income stream. Okay. So having said that, I will spend very few moments here talking about the bootcamp to close things off again, this if you're listening to this point, all this is able to be sponsored by the coders campus boot camp, available at coderscampus.com/bootcamp. This again, this is how now I'm able to move forward with creating all this content getting back to what I love. I love creating content. I love doing these podcasts. I love doing YouTube videos. Shout out to the YouTube channel. If you don't know I have a YouTube channel. Oh, please go and check it out. It's just you probably, as I say Google, go to YouTube and search for coders campus on YouTube and you should find me there. I've got a bunch of content coming out on YouTube right now. That is wonderful. If you have time to sit in front your computer and watch videos. So anyway, shout out to the YouTube channel. Go there subscribe. I don't have enough subscribers I have like 11,000 or something at this point. I want to get to that coveted 100,000 mile marker milestone. So if you could please do me a favor and subscribe and then reach out to about you know what is that 80 88,500 of your friends and get that subscribe. I've as well, I will be in debt, I will be completely indebted to you. I will you know what I will give you a free ride through the boot camp, if you can bring in 88,000 subscribers to my YouTube channel, I will give you a free ride through the whole boot camp, I will personally

25:16
bow down and anyway, yeah. So check out the YouTube channel and check out the bootcamp as well. So if your goal is to get a job as coder, I won't say anything else. But that it's a very, very compelling product, the students in there absolutely love it. I absolutely love doing it and seeing the students and the success that they get and the jobs they get and how their lives change, and so on. So that I think in the next episode of this podcast, we'll have an interview with one of our students, where he you know, completely raw, unedited, unplanned, I just sort of pick his brain, about his experience in the boot camp and give and he gives his honest feedback and thoughts about his experience of the boot camp and the outcome. So I think I'll be releasing that as the next episode, if not the next one, it should be soon. Check out that interview with one of my students. And I'm gonna be doing a bunch more of those interviews because I'm terrible at marketing. And I need to get better at marketing and getting students to, you know, I have prospective students, I have people like you listening right now, who reach out to me and ask me questions. And they say, Trevor, you know, there's not, there's not a whole lot on your boot camp. Right now, there's not a whole lot of stuff I can learn. There's not a lot of, you know, marketing stuff. And I don't see a lot of testimonials like what is it? Like? Is it bad what's going on? And is there I can, there's tons of people, I can pick that everyone would sing praises, I should say the ones who graduate we have about an 80% graduation rate. The 20% who don't graduate usually drop out in the first like 14 or 30 days because they realize that they bid off way more than they could chew with their schedule. And that's never a good outcome. But anyway, it's not because of the product is because the time commitment, that's why I try to do my best to seek out people who have the ability to commit the time and the effort to doing this bootcamp. Anyway. There's tons of people out there that will sing the praises of the boot camp and will say all the right things that you want to hear that you you know, because all you want, normally what everything that you want is to just get a job to be to get a job and to feel confident in that job. And that's exactly what the output is for this boot camp. Okay, it's not an easy process. It's, it's, it's grueling. I won't lie to you. And but there's tons and tons and tons of support built into it. So anyway, people say, you know, it's just a scam, like, you don't have anything on and that's because I am terrible at marketing. I'm a programmer, guys. I love teaching coding. And I love actually coding, I hate, you know, sitting down and writing out testimonials from students and like trance transcribing them. And anyway, so that's why I'm just saying, Hey, I'm gonna do an interview with these people that I can do that sort of comfort, that's a comfort, comfortable thing for me to sit down and interview past students. So that's what I will do. I will sit down interview past students, and you'll have long form interviews, to listen to, for testimonials. So you can get their raw, unedited, honest opinions about their experience. So having said that, please do check out the boot camp, if your goal is to get a job as a coder, and you've been trying to do it for like, you know, if you've been trying to do this for years, please, please stop trying, like, just join the boot camp, it is so much faster. And it's such a better experience, and so much less frustrating than trying to do it completely on your own. And you'll end up making more money in the long run. Okay, the boot camps are cheaper than trying to do it self taught. If it takes you more than 12 months to learn on your own. It would have been cheaper to just do a boot camp, okay with the increase in salary that you're getting that you will get. Anyway, okay, I will stop. I promised I wouldn't talk a long time and I'm already starting talk a long time. Thank you so much for listening. Hopefully, I will see you in the next episode where we are going to be diving into hopefully, like I said, an interview with one of my past students. And hopefully you really enjoyed that and I look forward to seeing you there. So take care of yourself. As always, happy learning. And bye for now.

29:02
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 coderscampus.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 coderscampus.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 coderscampus.com/review Happy Learning

 

Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.