DiscoverCoders Campus PodcastEP56 – Fun Tricks and Analytics with IntStreams in Java
EP56 – Fun Tricks and Analytics with IntStreams in Java

EP56 – Fun Tricks and Analytics with IntStreams in Java

Update: 2022-01-25
Share

Description


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 no

Comments 
loading
00:00
00:00
1.0x

0.5x

0.8x

1.0x

1.25x

1.5x

2.0x

3.0x

Sleep Timer

Off

End of Episode

5 Minutes

10 Minutes

15 Minutes

30 Minutes

45 Minutes

60 Minutes

120 Minutes

EP56 – Fun Tricks and Analytics with IntStreams in Java

EP56 – Fun Tricks and Analytics with IntStreams in Java

Trevor Page