Artwork

תוכן מסופק על ידי Trevor Page. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי Trevor Page או שותף פלטפורמת הפודקאסט שלו. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.
Player FM - אפליקציית פודקאסט
התחל במצב לא מקוון עם האפליקציה Player FM !

EP54 – What is the Map Operation in Java Streams?

41:49
 
שתפו
 

Manage episode 308083061 series 2603665
תוכן מסופק על ידי Trevor Page. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי Trevor Page או שותף פלטפורמת הפודקאסט שלו. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.

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

In this episode we'll talk about:

  • How the .map() operation in Streams is completely different than a HashMap
  • The details of how .map() will transform your data from one type upstream, to a different type downstream
  • A real world example of how to use the .map() operation in your code

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
o k, my fellow coders, welcome to this 54th episode of the coach campus podcast. Thank you so much for joining. As always, if you're in your car, thank you for listening to me while you're driving, if you are on the treadmill or something at the gym, thank you for listening to me while you are working if you are doing dishes, or if you are just relaxing on a couch or in front of your computer. Thank you so much for listening. This is one of the most enjoyable activities that I do as a part of what I have been doing for the last 10 years, which is teaching people how to code. And in today's episode, we're going to be diving into as promised the topic of maps, specifically the mapping or the map operation inside of streams. Now, right up front, I want to say just like I did last time in the in at the end of that last episode, streams, maps, rather sorry, map as an operation within a stream has nothing to do with a HashMap or the map interface in the Java language. Okay, nothing to do with it. So anytime that you hear me say stream and map together dot map, don't think of HashMaps Okay, that's gonna be hard, don't think key value just throw that concept away okay. I wish there was a better fitted word here that we could use for this operation. But think of this as an operation this is you are executing a method the method is called dot map, okay, that's the function of the method or the procedure or whatever it is that you want to call it, they all mean the same thing okay. This is an operation that we are performing it is not a data structure. Maps as data structures are something different dot map is an operation is a function that we are calling that we are executing. Okay, so I will say that, first and foremost, at the top of this episode. Now, what the heck is a map dot map this operation within streams. So that's what I want to dive into today. But quick interlude, as I'm publishing this episode, it is Black Friday, in 2021. So it's November 26 2021, is when I'm publishing this, or at least I'm planning on publishing it. Obviously, if you are listening to this, and you might be listening to it on this day, but you know, you're probably listening to it at some point in the future. But why this day is important, this Black Friday, November 26 2021, is we are releasing a brand new product. It is a brand new

3:00
coding boot camp that I am very proud of, as you've heard me talk about in past episodes, I am very bullish on boot camps. I am a believer, I am a I am I've been converted to boot camps. Okay, I'm a believer in their efficacy, the efficiency and the outcomes that come from boot camps now. But, Trevor, you already have a boot camp? Why are you talking about releasing a boot camp? Well, let me tell you the last, you know, three years that we've been doing boot camps, I have had to turn away a significant portion of students, aspiring coders potentially just like yourself, because you guys don't, or at least most of the people who have applied to our boot camp. They don't have enough prior knowledge, enough prior experience with code. And that leads to them. Probably not having a very good outcome inside of our full boot camp that exists today as it is and as we've been running for the past three years. So I've always felt a little bit icky about that. I don't want to turn people away and say you're not ready yet. So that is why I am announcing today, as you know, Black Friday, November 26 2021, we have designed a boot camp that is fit for the complete beginner. So if you are a complete beginner, or if you just don't feel ready enough to jump into the you know, the big boy pool or whatever you want to call it. If you've been you know only doing this whole code thing for you know, maybe less than six months or something like that. You've been dipping your toes in the waters of coding for less than six months. This boot camp is for you. So and it is a coding boot camp. It is a 16 week program where we will teach you the fundamentals of coding. Of course we focus on the Java language while doing that. But let me tell you, this is the hardest part of learning to code, the fundamentals. Okay, what I'm talking about today streams and maps, those are beyond fundamentals. Okay, so if you if you're confused about streams and maps and or just streams in general. Yeah, that's good. That's probably means that you're you still need work on the fundamentals, you need this new boot camp. Okay, this I call it the core Java boot camp. Okay, because we've talked about core Java concepts. Core, Java is another way to just say the fundamentals of the Java language. And really, this is the fundamentals of any coding language at all. Okay, these concepts can be applied concepts can be applied to pretty much any object oriented language. Okay, that's what we're learning in this bootcamp. And let me tell you, like I said, this is the hardest part, this is where 90% of the people who start will stop, they can't get a grasp of the fundamentals. And they tap out, and they walk away. And I don't blame them. Because learning on your own learning without mentorship, learning without accountability, without community without direction, curriculum, and constant access to support. Without that stuff, you are so likely to fail, like 95% more likely to fail if you go that route on your own, okay, that's why I am big on boot camps, they really helped to bridge that gap. And they really helped to give you the confidence that you need, when you need it most. Okay, so I'm very proud and very excited to announce this, I want to call it the core Java boot camp. But really, like I said, it's just a boot camp to learn the fundamentals to get further than 90 some odd percent of people who've ever tried to learn how to code. So if this is you, if this is resonating, if you've tried before and quit, and if you're just sort of dabbling, or half in, but you really want to be all in, this is the way to do it. And And hey, if you're listening to this in 2021, on the in the Black Friday season, that hey, even better, there's a discount right now that we have, I haven't done a discount or a sale in like four years, because I haven't had time. But we're doing that here. And now. So you can get in and get access to this coding boot camp at 25% off. And in my opinion, this is very affordable. Okay, so this is not, because it's we're not teaching the five pillars of full stack development in this bootcamp, we're just focusing on the fundamentals for 16 weeks, we can make it more affordable. So as part of Black Friday, you can get in for 1500 US dollars, okay. 15 101 time gives you access to the entire 16 weeks of the bootcamp it gives you access to, like I said, all the support that you're going to need the live calls, we have like three live calls a week, with our lead instructor, we you get assignments. So there's four super challenging assignments, and you get code reviews for those assignments, you'll be able to get personalized feedback on the code that you write. There are plenty of like coding exercises in there that are smaller than the assignments. The assignments are where you're going to really struggle. But the exercises are a lot easier. Again, this is all making sure that you are absorbing and learning what it is that is being taught as part of this curriculum. So there's probably more that I didn't mention, but oh, yeah, there's a Slack channel. So the whole point, again, everything that we can do to help you get very comfortable with the fundamentals of coding, that's what we are doing this boot camp for 1500 bucks. Okay, that is an insanely good deal if you know and understand the power of what a boot camp delivers. Okay, my other coding boot camp is almost 10 times that price. So hopefully you understand the the impact and the severity of this offer and how big this is. So if you are a beginner, if you've been desperately needing help and wanting guidance from experts to be able to hang out with people just like yourself to be able to ask questions when they come up to be able to get help when you need it most. And to learn from experts about coding conventions, and all the things that you should be doing in order to be ready to take the next step, the next level, which is the next boot camp, do all that stuff, get that help in here, be ready for the next boot camp, and then we take all the money that you invested in this one, and we just apply it 100% As a credit towards the next boot camp. Okay, and then that next boot camp, then you're the big, you know, you're playing with the big boys, you're, you know, the big guns, if you will, whatever you want to call it. That's the fully comprehensive full stack boot camp that you've already heard me talk about this whole time. Okay, so every penny that you invest into this new one that I'm talking about the core Java boot camp, you can reinvest it 100% right back into the next one. Okay, so you can just take that and use it as a credit full credit, full discount, whatever you want to call it. So, really, this is like a if you're long on this process on education and getting a job as a coder, this is kind of like free. It's like a free boot camp that you can take right? Because you're just gonna get all that money back in the next one. So that's why I'm super excited about it's kind of like getting free access to me, my brain, my staff's brain, the all the content, everything that I've worked 10 years to create, you're essentially getting it for free. If you plan on going, going the route the whole way and doing the full full stack boot camp afterwards. Okay, so hopefully that makes sense.

10:09
Yeah, plus, like, I didn't say you're guaranteed admittance into the other boot camp. So if you finish this one, like the new one, you'll be guaranteed admittance into our full boot camp so I don't have to turn you away, which feels so much better. So cool, if that's of interest. And again, if it's Black Friday, and you're listening to this now, and it hasn't hit whatever, November 30, at midnight is when we're turning off the discount, go to coderscampus.com/beginner, pause the podcast right now, if that's resonating, if you're driving right now, if you're I don't know what pull over like if this is, if this is what you've been wanting for so long, it is right in front of you. All you need to do is go to the site sign up, it's as easy as typing in you know, your name, your email address, and whatever, you know, payment, you want to do credit card, PayPal, whatever you want, do it. Okay. And if you can't do a single payment of 1500, that's too much. We have the payment plan as well. It's like, you know, for payments of 597 or something like that, or three or four payments. I don't know, go there, you can do the paid plan as well. In any case, coderscampus.com/beginner. Okay. All right. So I'm taking so much time there to explain that. And even if you're in the future, and this is not the Black Friday sale, again, even without the 25% discount, like I said, you can take 100% of that and apply it towards the next boot camp. So it's it's worth it either way, whatever amount you're paying, you're getting it right back in the next boot camp, and then that next boot camp, the full stack boot camp, the one that's huge, the one that's, you know, 10 times the size, that one has a job offer guarantee, as you've heard me talk about so well worth it. Okay. Let's get back to the content of hand, which is streams. Let me take a sip of water to get down from that. Okay, I'm ready. Let's do this. So Java streams, specifically, an intermediary step called dot map, again, reiterating, not HashMap. If you're just thinking hash map again, throw it out. No, not not this isn't this is a function, there's a method, there's an operation dot map. So what is dot map? Do Trevor get to the point. So dot map is an intermediary step in part of a stream. So as you're streaming, a collection of objects, dot map allows you to take the object that you are currently streaming over, in other words, looping over. Okay, so takes the object that you are looping over and allows you to perform operations on a smaller set of its properties. Okay, so that's essentially what it does allows you to narrow in on a smaller set of properties or a single property, if you will. Okay, so, okay, fair enough. So it's all about zooming into a more specific property inside of the object that you're iterating over. How is that helpful? What does that mean? Can you be more specific? Can you give me an example? Sure. Absolutely. That's what I'm here for. So the example that I have in my code that I am hopefully have published is an example of using like, something like bank transactions, this could be anything, right? This could be any object, any object that you've created that has properties. For the one that I've created, the one that I'm using for the example is a bank transaction object. So the bank transaction class has three properties. So a bank trend transaction has a transaction ID, it has a created date, like when you created the transaction when the transaction was made. And it has an amount, okay, a monetary amount. So a bank transaction is three things transaction ID created date, and amount. That's it. That's what our bank transaction has, which again, think about the real world, when you perform a transaction, you have your debit card or something or you have a credit card, like you go to coderscampus.com/beginner and sign up for the bootcamp right. Course, camps.com, Ford, you'd like that, that was pretty good. I was smooth, I didn't plan I didn't plan on that. You go there and you create a new bank account transaction, you sign up with your credit card, with PayPal, inserts a new bank transaction. And you're now part of the bootcamp. Congratulations. So you do that and you have a bank transaction, right. And that transaction will have a unique ID, because that's how the world works. You need unique IDs to zoom in on the single thing that just happened just like when you have a you know, a driver's license number. It's unique to you and your state or your province or wherever you live your region.

14:48
You need some way to identify that single thing. So we have a transaction ID. We have a created date, which is boom, you don't happen just now or whatever it is that the the transaction gets created. We can put that into it. To date, and then we have an amount, right? It's just 1500 bucks for the boot camp. So that's the amount, right? Or 597? Because one of the whatever for payments that you that you can make to join the boot camp, right? There you go. That's your amount. So that's what a bank transaction is right? There you go. We just talked about a real world case. So with that bank transaction, let's think what can we do with that data? Right, let's say we have a bunch of bank transactions, let's say you log into your bank account, and you want to list out the past three months of transactions. Okay, so cool. You open up a report, you say, you know, show me the last three months of data, and then your bank, banks website, or whatever, lists out every single transaction that has happened in the last, whatever, 30 or 90 days or whatever, right? Ooh, cool. So you see all them listed there? Well, that's kind of like a collection of bank account transactions, right? That's your, that's your bank transactions. There's a whole bunch of them, there's a collection of them, there's like 90 days worth of the transactions. Right? Okay, with me so far. So let's say we have, we want to do an operation on that, we want to get a some of the most recent transactions. Okay, so of the last 90 days, or the transactions, let's say we want to get a some of the most, you know, recent, seven days of transactions. Let's say that's what you want to do. Okay? I don't know why, let's just say that that's something that you want, you want to get a sum of the last seven days of your transactions, because maybe you want to keep keep track of your weekly spending or something and you want to chart it or something. Fair enough. Cool. So how will we do that? Well, again, old way versus new way, there's an old way, which is without using streams, and I will be as brief as I can about this. So the old way, what would you do? Well, you'd have to get your you know, your list of bank transactions and pass it into a method, this method can be called, you know, get some of recent transactions, let's say, Cool, that's no different from using streams, we need a method, let's create a method get some of recent transactions. And we pass in our collection of bank transactions. In this case, we can use a list of bank transaction objects, and pass it into the method. Great. So how can we then get a sum of the most recent transactions? Well, we need to say, Okay, we want to get transactions between today and seven days ago. So you'll need a little operation to generate to the date range or whatever. Again, I don't want to get into the details of that, you'll have to do that either way. So cool. Now, specifically, in order to make this work, we need to iterate through every single one of the bank transactions. And we need to check and see if the created date for that transaction is between now and set, let's say seven days ago, right. So in other words, this is a filter step, we're not mapping yet, this is just a filter step. So we have to filter out and say, Okay, give me the transactions for the past seven days and add them to my to my list of recent transactions, because we will have created a list of recent transactions or something, a list of bank transactions called recent transactions, we add them to that list. Great done. So now we have a new ArrayList of recent transactions, meaning transactions that have happened within seven days ago. But now we want to get a sum of all of those transactions. So now we've collected them all. Got them all in a list, all the recent ones, just just the ones for the past seven days, we want to know add them up, right, so we need to get a sum. So we need to declare a variable called sum. And it's a double because I'm using a Double data type. We started at $0.00 0.0. And now we need to iterate through. So we do another for loop to iterate through the most recent transaction or so I should, I should say, the recent transactions list. This is a new list that we instantiate it as part of this method that we're talking about here. So we instantiate a new list. And now we say, hey, within that new list that we're iterating through recent transactions, each one of those is going to be assigned, I'm using a for each loop. So it gets assigned to a new variable called a recent transaction. So the singular version that's just good coding convention, singular versus plural plural for a collection. So we have a recent transaction. This recent transaction has an amount so we say recent transaction dot get amount, because we want to get that amount. And we want to add that into our sum. So we have to say sum equals the sum plus recent transaction dot get amount.

19:39
Okay, so that's what we're doing here. We're just saying hey, we have an object called a recent transaction, it is a bank transaction. The bank transaction has several properties, but we only care about one of those properties which is amount. So we need to say recent transaction get amount and then we take that amount and we add it to the value of some the previous values and reassign it back to sum. So in other words, we're going to do sum equals sum plus recent transaction dot get amount, which is what I just said. But hey, I wanted to just zoom in on the fact that we only care about one of the bank transactions properties, get them out, or sorry, amount, and we get that through the method called get amount. Okay? So we add up the sum, and then hey, we return the sum from this method, right? Because we want to get the sum of all the transactions so we can return that sum. And we are done. Okay, that's the old way of doing it. So to summarize, the old way of doing it, we need to instantiate a new list, we need to populate that new ArrayList with the most recent transactions, we need to initialize a some variable and initialize that 0.0. And then we need to iterate through the new list that we just created called recent transactions. And then we need to for each of those transactions, get the amount and add it to sum, and then we need to return some. Okay, that's sort of what we're doing in the old way. Okay, it's about what is this? 1-234-567-8910 1112 lines of code? Okay. So 12 lines of code, we can get her done? Well, in the new way, we only need 123456. So we need half as many lines of code. Cool, right? So how do we do it the new way, right? Okay, same thing, we have a method, the method can be called Get some recent transactions, we pass in our list of all the transactions from you know, whatever, the last 90 days, just like before, nothing changes here, all good. And we still have to define our window. So we have a variable that we create, called, I call it seven days is the name of the variable. Again, it's just what's our window of time that we're dealing with here. So we'll look at a seven day window. So we still need to declare that variable, that's still, that doesn't change just like we did in the old way, we have to do it in the new way as well, because that's part of our filter that we're going to use now. Where it changes is we can say bank account, or sorry, bank transactions, which is all of the bank transactions that we're passing in here, bank transactions dot stream. Okay, so we're going to stream our bank transactions. And then as you expect, as I alluded to, before, you need to filter this list of bank transactions, just like we talked about last episode, so I won't spend too much time here, we're filtering by created date. So we're saying, you know, dot filter, or we're having the lambda the lambda for filter is a predicate, the predicate takes one input. So the input that we're going to call it is just called bank transaction, you can call it whatever you like, as my followers know, I am partial to using the variable dog poop, to define a variable such that it can be named whatever the heck you want it to be named, the name doesn't matter. You could call it dog poop if you wanted to. But obviously, following good coding conventions, we give it a better, more meaningful name. So the meaningful name for the variable being passed into this predicate functional interface is going to be called bank transaction, because that's what it is, ladies and gentlemen, it's a bank transaction. So again, we're streaming we say dot filter, bank transaction is the name of the input variable to this lambda expression. We have our lamda arrow, and then to the right of the lambda arrow, we have the, the body right, so the body is just going to say bank transaction, which is the name of the variable, we just green dot get created. date.is, after seven days is the name of our variable. Now, I skipped over those details in the old example. But that's exactly what we had to do. In the old example, we just say, hey, is this bank transactions created date after seven days ago, then cool that included in this new list, but here in the new way, we just do a dot filter, we don't have to create a new list and populate it, we just use dot filter. Cool. So Dodd filter done now remember, dot filter returns, because it's an intermediary step, it returns another stream, specifically a stream of bank transaction objects. So dot filter still returns a stream? What can you do on a stream? Well, you can say dot map.

24:15
Okay, so you can do method chaining. I talked a little bit about method chaining. So we can do some method chaining here dot map. Well, what does dot map take in as a an input? It what does it take, you don't even know it takes in a function. So a function functional interface or whatever you whatever you want to call it. So it takes a type t, and it returns a Type R. Okay, whatever remember to return type is and it figures out what that return type will be based on what you give it, as well as the input type. So again, dot map takes an input type T. In this case, it's our bank transaction, because that's what's being passed to it from above from the filter above. So we have a bank transaction type. We'll call it bank transaction as part of this lambda function. Again, we could call it dog poop if we want it to be called X called Zed or whatever variable. But again, it's a bank transaction. So call it bank transaction, lambda arrow to the right of the lambda arrow, what are we doing? Well, again, dot map, takes an object bank transaction, and returns a specific like a subset of its properties, or, well, a single property, typically, in the real world use a single property. To return a subset of its properties, you'd need to have like a DTO, or something, but we won't go there. In this, in this example, we're just using a single property. And that's pretty much most of the time what you see with this operation. So we're gonna say bank transaction dot get amount. Okay, so just to reiterate, we had a filter, and then we have dot map, bank transaction on the left, lambda in the middle, bank transaction dot get amount on the right. So we're just going to say, hey, take the input of a, an actual bank transaction object and sort of coming in and spit out an amount as the output. Remember, the backing functional interface for dot map is function, function, input type T, output type R, okay, T R or generics. T in this case is bank transaction R in this case will be double, y is the output type double because we are telling it bank transaction dot get amount, what is the data type of get amount, the data type or the get amount data type, what's returned from that is of type double, because the amount is of type, double input type, bank transaction, output type double. So we've just translated if you will, in our stream, I like to say I like to use the expression upstream. In other words above this operation that we just did. I don't know if that's actually a thing, what they say makes a lot of sense if they call it upstream. But anyway, I say upstream. So upstream from the dot map operation, we were receiving bank transaction objects. But now downstream from the dot map operation, we've just changed that into double data types. So above map, we had bank transaction below map now downstream, we're going to have double. So that changes things. Alright, because we're no longer working with bank transaction as an object type anymore. We're now dealing with double. Okay, we have just mapped it to be something different, we've changed it, we transformed it, it's almost like dot transform, have you better name the dot map. But anyway, we've transformed it from one input to a different output from bank transaction as an input to double as the output. So now downstream below, with the next operation, we're dealing with double doubles. Now we're no longer dealing with bank transaction. As a type, we're dealing with the the amounts that belong to those bank transactions, downstream below dot map after dot map happens. So after the dot map, operation happens, picture each of the bank transactions for last seven days going in, think of your bank account and all that information you see on the screen, the last seven days, all the transactions, you know, credits and debits and from, you know, this grocery store, and here's your paycheck. And here's like all these transactions, all the details, you know, the dates that they happened, the name of the retailer, or whatever, the as well as the amounts in or out and all like there's all this information, but then we when we do dot map, which is like transforming it. Now just picture yourself zooming the screen in, specifically on the amount.

28:55
Column, all we're looking at now is the amount, okay, there's still a bunch of them still a bunch of amounts, and they are from all of those transactions on the screen, you're just throwing away all the other data around the screen, and you're just zooming in to the amount. That's what we're doing. We're zooming in on one property, one piece of data amount. We're throwing everything else away, so to speak. Okay, so we haven't changed anything, we haven't changed the underlying transactions at all, that the bank transaction objects are still sort of there. It's still those objects. But now all we can work with is just the amount in other words, just the double data types for amounts for those transactions. Okay, so that's important understand, downstream after we do the dot map, we're transforming it from one thing into something completely different. Okay. And that's where students often get mixed up. And they think, Oh, when I do a dot map, all of a sudden now I've lost my object. I don't know where the whole object went. All I have is like a piece of the object and I say exactly That's what you're telling it to do. That's what dot map does. You're, you're sort of throwing away the rest of the data. You're just zooming in on that one property. Okay? Cool. Let me take a sip. So hopefully I've over explained that hopefully you make it makes some sense to you, you and your mind. But remember, I should say, remember, let me tell you, what is dot map return. Remember, dot filter returns a stream of the object that we're iterating over right? The stream of Well, in this case, it was the stream of bank transaction objects. But what is the dot map return? Any guesses as to what dot map returns? Again, the two hints are it's an intermediary operation. And it underlying it, it has the function with T as an input and R as an output. So because dot map is an intermediary operation, it returns another stream. Okay, that's what intermediary operations do they just return another stream of something? So because it's an intermediary step, we have a stream of something. But what is the of something? What is the something I should say? Right, the something is now different, we don't have the object anymore, the Shinsei object, we don't have the original object, which is bank transaction. Remember, the underlying functional interface here is a function which takes a type T the bank transaction, and the output type OMA output type R, which in this case is double. Remember, we're doing an amount we're outputting an amount bank transaction dot get amount. So because we're doing get amount and amount as a double, the output now is a stream of double objects. It's no longer a stream of bank transaction objects, we have mapped it, we have transformed it to be a stream of double objects. And that has, I guess, consequences. I don't I don't wanna say consequences that has implications. Okay, we are no longer dealing with the bank transaction objects, we are now dealing with a native data type to Java, Java knows doubles. And because Java knows doubles, we can do something with that. Okay, there's special operations that we can do with double. So let's talk about that. What can we do with it? Well, the next step, we're going to do a terminating operation, a terminal operation, we're going to do dot collect. So we're going to collect all of those double amounts, right, all those bank transaction amounts, not all those bank transactions, are collecting all the bank transaction amounts that were from the last seven days, that's what this these steps have done. We filter get the last seven days we map, turn it from a drip bank transaction into the bank transit transactions amount, and then collect it. So we're collecting the last seven days worth of bank transaction amounts. Okay, what are we doing with collect? Well, we're gonna do something special ladies, gentlemen, we're going to do collectors dot, summarizing double. Now this I'm pulling out of my butt, this I did have to Google. But there's a special collector called summarizing. And then there's a bunch of different summarizing operations, you can do summarizing int, I think you can do you can do summarizing in summarizing double and summarizing the long. Okay, so this is a, this is a method that we're invoking. And you can pass in the amount so it just takes in summarizing double takes in

33:37
a what's called a to double function, but it's just like a function where you pass in a mount. So in other words, the summarising double just, you pass it into the mount and then you just on the on the left of the lambda and on the right of lambda, you just have a mount, you're not going to change it, you're not doing anything crazy with it, I just have a MT arrow amount, which looks kind of weird, but that's what you're doing. You're just saying don't do anything special with it, just take the double that's coming in from upstream from dot map, and just use it right. So summarizing double, returns something special, it returns something called Double summary statistics object. And this double summary statistics object which is returned its as opposed to collecting it to like a list. In the past example we said collectors.to list to take everything and turn it into a list. Now we're turning the bank transaction amounts into something called a double summary statistics object. Why are we doing this? Well, because the double summary statistics object which I just called the summary statistics object, gives us access to some really cool methods we can do dot get average get max get min get some we can do and then to do more, we can do a bunch of stuff with it. Okay. So, the the the important things to note there is that You can do get average get max get min get some average max min, some. Okay, those are for Super you oh and count. So I forgot count, average max min sum count. That's what this thing this summary statistics thing does for us. Okay, it unlocks a bunch of cool mathematical stuff for us to work with. Okay, so that's what the summary Summary Statistics does. And we were able to do that because we did the dot map step where we returned a double amount from the list of transactions that we're dealing with. And then we collected it into something called summarizing double what we use, collectors dot summarizing double, which returns a double summary statistics object. I know there's a lot of words going around there. But the point is, we can have this summary statistics summary statistics object and say dot get some, in other words, we can return dot get some. And that's it, we've now collected the sum of bank transactions, the bank transaction amounts from the last seven days, we return the sum, and we're done. Now the beauty with with this is we could also return the min and the max, the average and the count. If for some reason those were helpful, we could have done those as well imagine the old way what you would the work that you'd have to go through and the code that you have to type out to get the min, the max, the average sum and the count, right, that would be like that would take you all day, potentially, I don't know who knows. And now we just have it all in this one beautiful little operation called collectors dot summarizing double. So it's beautiful. No, just kiss to this. Streams stuff brought to you by dot map, being able to take an input type and trance, transform it into a different output type. Okay, that's what dot map does, again, not a great name, I prefer dot transform. I don't know what a better name would be then than that. But anyway, nothing to do with a hash map. That's something completely different. This transforms inputs into a different output. So that downstream, you can do something with it, like we saw here. Now when we take we zoom in on the bank transaction amounts,

37:14
we can do something with that. Now. caveat to everything I just said, you could probably get away with not using dot map, you could probably just done collectors dot summarizing double the input would be bank transaction, and the output probably could have been bank transaction get amount. So I'm kind of cheating here because I wanted to show you the dot map and explain dot map step. But that's a great way to explain the dot map step just goes to show you can make this five lines of code instead of six if you really wanted to stream to the best. That's me dunking on the old way of for loops, and while loops. And I hope you've learned something valuable here today. So hopefully, this was fun. Again, if you're listening at any realm close to 2021 and 2022. And you are a fairly new beginner to this whole coding thing. And your end goal is eventually to get a job as a coder, I really strongly suggest you check out our brand new Java fundamentals core Java boot camp that I just introduced as of today, as of the recording of this lesson or this episode. coderscampus.com/beginner will get you into you don't even have to like apply for it. Okay, there's no application process. We are accepting everyone. Okay, so Well, I should say so long as you have about 20 hours a week. Maybe we shouldn't accept everyone if you don't feel if you can only put in like four hours a week here. Yeah, then you're not committed. So yeah, you should have 20 hours a week to get to this. But as of right now, the recording we actually don't have an application process. So yeah, join coderscampus.com/beginner. And I look forward to seeing you on the inside of this 16 week, Java or core Java coding boot camp where you are going to learn the fundamentals of the Java language and you are going to do for crazy hard challenging assignments. For someone who's a beginner, you're going to get four beautiful code reviews, explaining everything that you've done, right, everything needs improvement, any conventions that you're not following, picking up on any bad habits and correcting them right there. And then it's beautiful process. With this, the code reviews are the best, I love them. Plus, you're also gonna be able to hang out possibly with me three days a week, most likely with one of my lead instructors. But hey, I often pop into those calls every once in a while and say hi to my coders and private Slack channel, you'll be able to get access to me there pretty much every single day seven days a week. As long as I'm awake and my wife is not within earshot or when I look I will respond to messages unless I need to be a dad and do family time then I will put my phone down but other than that I am responding to Slack messages and helping you guys out to achieve your goal of ultimately getting a job as a coder. So this is this is the best place to start if you are a beginner coderscampus.com/beginner Check it out. I'm super excited about it 25% off today until Monday, all was it November. What is Monday, November 29. My Calendar. Yeah, until the end of day, Monday, November 29, which is pretty pretty much like the morning of Tuesday, November 30 2021, you can get 25% off. But like I said, even if you don't get that discount, before time expires, 100% of what you've paid will go as a credit towards the next bootcamp, which is the full bootcamp which is the one that guarantees you that job. Okay, so it's pretty much like you're getting a free boot camp for 16 weeks, and you get to hang out with cool, cool people, just like yourself, and hey, me, too. So hopefully, I will see you there. I think it's one of the most amazing deals I've ever put out. And I'm truly proud to say that I'm able to release this bootcamp. So can we see on the inside coderscampus.com/beginner, and I can't wait to see you in the next episode. Take care of yourself. Happy learning. And bye for now.

41:03
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 EP54 – What is the Map Operation in Java Streams? appeared first on Coders Campus.

  continue reading

64 פרקים

Artwork
iconשתפו
 
Manage episode 308083061 series 2603665
תוכן מסופק על ידי Trevor Page. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי Trevor Page או שותף פלטפורמת הפודקאסט שלו. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.

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

In this episode we'll talk about:

  • How the .map() operation in Streams is completely different than a HashMap
  • The details of how .map() will transform your data from one type upstream, to a different type downstream
  • A real world example of how to use the .map() operation in your code

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
o k, my fellow coders, welcome to this 54th episode of the coach campus podcast. Thank you so much for joining. As always, if you're in your car, thank you for listening to me while you're driving, if you are on the treadmill or something at the gym, thank you for listening to me while you are working if you are doing dishes, or if you are just relaxing on a couch or in front of your computer. Thank you so much for listening. This is one of the most enjoyable activities that I do as a part of what I have been doing for the last 10 years, which is teaching people how to code. And in today's episode, we're going to be diving into as promised the topic of maps, specifically the mapping or the map operation inside of streams. Now, right up front, I want to say just like I did last time in the in at the end of that last episode, streams, maps, rather sorry, map as an operation within a stream has nothing to do with a HashMap or the map interface in the Java language. Okay, nothing to do with it. So anytime that you hear me say stream and map together dot map, don't think of HashMaps Okay, that's gonna be hard, don't think key value just throw that concept away okay. I wish there was a better fitted word here that we could use for this operation. But think of this as an operation this is you are executing a method the method is called dot map, okay, that's the function of the method or the procedure or whatever it is that you want to call it, they all mean the same thing okay. This is an operation that we are performing it is not a data structure. Maps as data structures are something different dot map is an operation is a function that we are calling that we are executing. Okay, so I will say that, first and foremost, at the top of this episode. Now, what the heck is a map dot map this operation within streams. So that's what I want to dive into today. But quick interlude, as I'm publishing this episode, it is Black Friday, in 2021. So it's November 26 2021, is when I'm publishing this, or at least I'm planning on publishing it. Obviously, if you are listening to this, and you might be listening to it on this day, but you know, you're probably listening to it at some point in the future. But why this day is important, this Black Friday, November 26 2021, is we are releasing a brand new product. It is a brand new

3:00
coding boot camp that I am very proud of, as you've heard me talk about in past episodes, I am very bullish on boot camps. I am a believer, I am a I am I've been converted to boot camps. Okay, I'm a believer in their efficacy, the efficiency and the outcomes that come from boot camps now. But, Trevor, you already have a boot camp? Why are you talking about releasing a boot camp? Well, let me tell you the last, you know, three years that we've been doing boot camps, I have had to turn away a significant portion of students, aspiring coders potentially just like yourself, because you guys don't, or at least most of the people who have applied to our boot camp. They don't have enough prior knowledge, enough prior experience with code. And that leads to them. Probably not having a very good outcome inside of our full boot camp that exists today as it is and as we've been running for the past three years. So I've always felt a little bit icky about that. I don't want to turn people away and say you're not ready yet. So that is why I am announcing today, as you know, Black Friday, November 26 2021, we have designed a boot camp that is fit for the complete beginner. So if you are a complete beginner, or if you just don't feel ready enough to jump into the you know, the big boy pool or whatever you want to call it. If you've been you know only doing this whole code thing for you know, maybe less than six months or something like that. You've been dipping your toes in the waters of coding for less than six months. This boot camp is for you. So and it is a coding boot camp. It is a 16 week program where we will teach you the fundamentals of coding. Of course we focus on the Java language while doing that. But let me tell you, this is the hardest part of learning to code, the fundamentals. Okay, what I'm talking about today streams and maps, those are beyond fundamentals. Okay, so if you if you're confused about streams and maps and or just streams in general. Yeah, that's good. That's probably means that you're you still need work on the fundamentals, you need this new boot camp. Okay, this I call it the core Java boot camp. Okay, because we've talked about core Java concepts. Core, Java is another way to just say the fundamentals of the Java language. And really, this is the fundamentals of any coding language at all. Okay, these concepts can be applied concepts can be applied to pretty much any object oriented language. Okay, that's what we're learning in this bootcamp. And let me tell you, like I said, this is the hardest part, this is where 90% of the people who start will stop, they can't get a grasp of the fundamentals. And they tap out, and they walk away. And I don't blame them. Because learning on your own learning without mentorship, learning without accountability, without community without direction, curriculum, and constant access to support. Without that stuff, you are so likely to fail, like 95% more likely to fail if you go that route on your own, okay, that's why I am big on boot camps, they really helped to bridge that gap. And they really helped to give you the confidence that you need, when you need it most. Okay, so I'm very proud and very excited to announce this, I want to call it the core Java boot camp. But really, like I said, it's just a boot camp to learn the fundamentals to get further than 90 some odd percent of people who've ever tried to learn how to code. So if this is you, if this is resonating, if you've tried before and quit, and if you're just sort of dabbling, or half in, but you really want to be all in, this is the way to do it. And And hey, if you're listening to this in 2021, on the in the Black Friday season, that hey, even better, there's a discount right now that we have, I haven't done a discount or a sale in like four years, because I haven't had time. But we're doing that here. And now. So you can get in and get access to this coding boot camp at 25% off. And in my opinion, this is very affordable. Okay, so this is not, because it's we're not teaching the five pillars of full stack development in this bootcamp, we're just focusing on the fundamentals for 16 weeks, we can make it more affordable. So as part of Black Friday, you can get in for 1500 US dollars, okay. 15 101 time gives you access to the entire 16 weeks of the bootcamp it gives you access to, like I said, all the support that you're going to need the live calls, we have like three live calls a week, with our lead instructor, we you get assignments. So there's four super challenging assignments, and you get code reviews for those assignments, you'll be able to get personalized feedback on the code that you write. There are plenty of like coding exercises in there that are smaller than the assignments. The assignments are where you're going to really struggle. But the exercises are a lot easier. Again, this is all making sure that you are absorbing and learning what it is that is being taught as part of this curriculum. So there's probably more that I didn't mention, but oh, yeah, there's a Slack channel. So the whole point, again, everything that we can do to help you get very comfortable with the fundamentals of coding, that's what we are doing this boot camp for 1500 bucks. Okay, that is an insanely good deal if you know and understand the power of what a boot camp delivers. Okay, my other coding boot camp is almost 10 times that price. So hopefully you understand the the impact and the severity of this offer and how big this is. So if you are a beginner, if you've been desperately needing help and wanting guidance from experts to be able to hang out with people just like yourself to be able to ask questions when they come up to be able to get help when you need it most. And to learn from experts about coding conventions, and all the things that you should be doing in order to be ready to take the next step, the next level, which is the next boot camp, do all that stuff, get that help in here, be ready for the next boot camp, and then we take all the money that you invested in this one, and we just apply it 100% As a credit towards the next boot camp. Okay, and then that next boot camp, then you're the big, you know, you're playing with the big boys, you're, you know, the big guns, if you will, whatever you want to call it. That's the fully comprehensive full stack boot camp that you've already heard me talk about this whole time. Okay, so every penny that you invest into this new one that I'm talking about the core Java boot camp, you can reinvest it 100% right back into the next one. Okay, so you can just take that and use it as a credit full credit, full discount, whatever you want to call it. So, really, this is like a if you're long on this process on education and getting a job as a coder, this is kind of like free. It's like a free boot camp that you can take right? Because you're just gonna get all that money back in the next one. So that's why I'm super excited about it's kind of like getting free access to me, my brain, my staff's brain, the all the content, everything that I've worked 10 years to create, you're essentially getting it for free. If you plan on going, going the route the whole way and doing the full full stack boot camp afterwards. Okay, so hopefully that makes sense.

10:09
Yeah, plus, like, I didn't say you're guaranteed admittance into the other boot camp. So if you finish this one, like the new one, you'll be guaranteed admittance into our full boot camp so I don't have to turn you away, which feels so much better. So cool, if that's of interest. And again, if it's Black Friday, and you're listening to this now, and it hasn't hit whatever, November 30, at midnight is when we're turning off the discount, go to coderscampus.com/beginner, pause the podcast right now, if that's resonating, if you're driving right now, if you're I don't know what pull over like if this is, if this is what you've been wanting for so long, it is right in front of you. All you need to do is go to the site sign up, it's as easy as typing in you know, your name, your email address, and whatever, you know, payment, you want to do credit card, PayPal, whatever you want, do it. Okay. And if you can't do a single payment of 1500, that's too much. We have the payment plan as well. It's like, you know, for payments of 597 or something like that, or three or four payments. I don't know, go there, you can do the paid plan as well. In any case, coderscampus.com/beginner. Okay. All right. So I'm taking so much time there to explain that. And even if you're in the future, and this is not the Black Friday sale, again, even without the 25% discount, like I said, you can take 100% of that and apply it towards the next boot camp. So it's it's worth it either way, whatever amount you're paying, you're getting it right back in the next boot camp, and then that next boot camp, the full stack boot camp, the one that's huge, the one that's, you know, 10 times the size, that one has a job offer guarantee, as you've heard me talk about so well worth it. Okay. Let's get back to the content of hand, which is streams. Let me take a sip of water to get down from that. Okay, I'm ready. Let's do this. So Java streams, specifically, an intermediary step called dot map, again, reiterating, not HashMap. If you're just thinking hash map again, throw it out. No, not not this isn't this is a function, there's a method, there's an operation dot map. So what is dot map? Do Trevor get to the point. So dot map is an intermediary step in part of a stream. So as you're streaming, a collection of objects, dot map allows you to take the object that you are currently streaming over, in other words, looping over. Okay, so takes the object that you are looping over and allows you to perform operations on a smaller set of its properties. Okay, so that's essentially what it does allows you to narrow in on a smaller set of properties or a single property, if you will. Okay, so, okay, fair enough. So it's all about zooming into a more specific property inside of the object that you're iterating over. How is that helpful? What does that mean? Can you be more specific? Can you give me an example? Sure. Absolutely. That's what I'm here for. So the example that I have in my code that I am hopefully have published is an example of using like, something like bank transactions, this could be anything, right? This could be any object, any object that you've created that has properties. For the one that I've created, the one that I'm using for the example is a bank transaction object. So the bank transaction class has three properties. So a bank trend transaction has a transaction ID, it has a created date, like when you created the transaction when the transaction was made. And it has an amount, okay, a monetary amount. So a bank transaction is three things transaction ID created date, and amount. That's it. That's what our bank transaction has, which again, think about the real world, when you perform a transaction, you have your debit card or something or you have a credit card, like you go to coderscampus.com/beginner and sign up for the bootcamp right. Course, camps.com, Ford, you'd like that, that was pretty good. I was smooth, I didn't plan I didn't plan on that. You go there and you create a new bank account transaction, you sign up with your credit card, with PayPal, inserts a new bank transaction. And you're now part of the bootcamp. Congratulations. So you do that and you have a bank transaction, right. And that transaction will have a unique ID, because that's how the world works. You need unique IDs to zoom in on the single thing that just happened just like when you have a you know, a driver's license number. It's unique to you and your state or your province or wherever you live your region.

14:48
You need some way to identify that single thing. So we have a transaction ID. We have a created date, which is boom, you don't happen just now or whatever it is that the the transaction gets created. We can put that into it. To date, and then we have an amount, right? It's just 1500 bucks for the boot camp. So that's the amount, right? Or 597? Because one of the whatever for payments that you that you can make to join the boot camp, right? There you go. That's your amount. So that's what a bank transaction is right? There you go. We just talked about a real world case. So with that bank transaction, let's think what can we do with that data? Right, let's say we have a bunch of bank transactions, let's say you log into your bank account, and you want to list out the past three months of transactions. Okay, so cool. You open up a report, you say, you know, show me the last three months of data, and then your bank, banks website, or whatever, lists out every single transaction that has happened in the last, whatever, 30 or 90 days or whatever, right? Ooh, cool. So you see all them listed there? Well, that's kind of like a collection of bank account transactions, right? That's your, that's your bank transactions. There's a whole bunch of them, there's a collection of them, there's like 90 days worth of the transactions. Right? Okay, with me so far. So let's say we have, we want to do an operation on that, we want to get a some of the most recent transactions. Okay, so of the last 90 days, or the transactions, let's say we want to get a some of the most, you know, recent, seven days of transactions. Let's say that's what you want to do. Okay? I don't know why, let's just say that that's something that you want, you want to get a sum of the last seven days of your transactions, because maybe you want to keep keep track of your weekly spending or something and you want to chart it or something. Fair enough. Cool. So how will we do that? Well, again, old way versus new way, there's an old way, which is without using streams, and I will be as brief as I can about this. So the old way, what would you do? Well, you'd have to get your you know, your list of bank transactions and pass it into a method, this method can be called, you know, get some of recent transactions, let's say, Cool, that's no different from using streams, we need a method, let's create a method get some of recent transactions. And we pass in our collection of bank transactions. In this case, we can use a list of bank transaction objects, and pass it into the method. Great. So how can we then get a sum of the most recent transactions? Well, we need to say, Okay, we want to get transactions between today and seven days ago. So you'll need a little operation to generate to the date range or whatever. Again, I don't want to get into the details of that, you'll have to do that either way. So cool. Now, specifically, in order to make this work, we need to iterate through every single one of the bank transactions. And we need to check and see if the created date for that transaction is between now and set, let's say seven days ago, right. So in other words, this is a filter step, we're not mapping yet, this is just a filter step. So we have to filter out and say, Okay, give me the transactions for the past seven days and add them to my to my list of recent transactions, because we will have created a list of recent transactions or something, a list of bank transactions called recent transactions, we add them to that list. Great done. So now we have a new ArrayList of recent transactions, meaning transactions that have happened within seven days ago. But now we want to get a sum of all of those transactions. So now we've collected them all. Got them all in a list, all the recent ones, just just the ones for the past seven days, we want to know add them up, right, so we need to get a sum. So we need to declare a variable called sum. And it's a double because I'm using a Double data type. We started at $0.00 0.0. And now we need to iterate through. So we do another for loop to iterate through the most recent transaction or so I should, I should say, the recent transactions list. This is a new list that we instantiate it as part of this method that we're talking about here. So we instantiate a new list. And now we say, hey, within that new list that we're iterating through recent transactions, each one of those is going to be assigned, I'm using a for each loop. So it gets assigned to a new variable called a recent transaction. So the singular version that's just good coding convention, singular versus plural plural for a collection. So we have a recent transaction. This recent transaction has an amount so we say recent transaction dot get amount, because we want to get that amount. And we want to add that into our sum. So we have to say sum equals the sum plus recent transaction dot get amount.

19:39
Okay, so that's what we're doing here. We're just saying hey, we have an object called a recent transaction, it is a bank transaction. The bank transaction has several properties, but we only care about one of those properties which is amount. So we need to say recent transaction get amount and then we take that amount and we add it to the value of some the previous values and reassign it back to sum. So in other words, we're going to do sum equals sum plus recent transaction dot get amount, which is what I just said. But hey, I wanted to just zoom in on the fact that we only care about one of the bank transactions properties, get them out, or sorry, amount, and we get that through the method called get amount. Okay? So we add up the sum, and then hey, we return the sum from this method, right? Because we want to get the sum of all the transactions so we can return that sum. And we are done. Okay, that's the old way of doing it. So to summarize, the old way of doing it, we need to instantiate a new list, we need to populate that new ArrayList with the most recent transactions, we need to initialize a some variable and initialize that 0.0. And then we need to iterate through the new list that we just created called recent transactions. And then we need to for each of those transactions, get the amount and add it to sum, and then we need to return some. Okay, that's sort of what we're doing in the old way. Okay, it's about what is this? 1-234-567-8910 1112 lines of code? Okay. So 12 lines of code, we can get her done? Well, in the new way, we only need 123456. So we need half as many lines of code. Cool, right? So how do we do it the new way, right? Okay, same thing, we have a method, the method can be called Get some recent transactions, we pass in our list of all the transactions from you know, whatever, the last 90 days, just like before, nothing changes here, all good. And we still have to define our window. So we have a variable that we create, called, I call it seven days is the name of the variable. Again, it's just what's our window of time that we're dealing with here. So we'll look at a seven day window. So we still need to declare that variable, that's still, that doesn't change just like we did in the old way, we have to do it in the new way as well, because that's part of our filter that we're going to use now. Where it changes is we can say bank account, or sorry, bank transactions, which is all of the bank transactions that we're passing in here, bank transactions dot stream. Okay, so we're going to stream our bank transactions. And then as you expect, as I alluded to, before, you need to filter this list of bank transactions, just like we talked about last episode, so I won't spend too much time here, we're filtering by created date. So we're saying, you know, dot filter, or we're having the lambda the lambda for filter is a predicate, the predicate takes one input. So the input that we're going to call it is just called bank transaction, you can call it whatever you like, as my followers know, I am partial to using the variable dog poop, to define a variable such that it can be named whatever the heck you want it to be named, the name doesn't matter. You could call it dog poop if you wanted to. But obviously, following good coding conventions, we give it a better, more meaningful name. So the meaningful name for the variable being passed into this predicate functional interface is going to be called bank transaction, because that's what it is, ladies and gentlemen, it's a bank transaction. So again, we're streaming we say dot filter, bank transaction is the name of the input variable to this lambda expression. We have our lamda arrow, and then to the right of the lambda arrow, we have the, the body right, so the body is just going to say bank transaction, which is the name of the variable, we just green dot get created. date.is, after seven days is the name of our variable. Now, I skipped over those details in the old example. But that's exactly what we had to do. In the old example, we just say, hey, is this bank transactions created date after seven days ago, then cool that included in this new list, but here in the new way, we just do a dot filter, we don't have to create a new list and populate it, we just use dot filter. Cool. So Dodd filter done now remember, dot filter returns, because it's an intermediary step, it returns another stream, specifically a stream of bank transaction objects. So dot filter still returns a stream? What can you do on a stream? Well, you can say dot map.

24:15
Okay, so you can do method chaining. I talked a little bit about method chaining. So we can do some method chaining here dot map. Well, what does dot map take in as a an input? It what does it take, you don't even know it takes in a function. So a function functional interface or whatever you whatever you want to call it. So it takes a type t, and it returns a Type R. Okay, whatever remember to return type is and it figures out what that return type will be based on what you give it, as well as the input type. So again, dot map takes an input type T. In this case, it's our bank transaction, because that's what's being passed to it from above from the filter above. So we have a bank transaction type. We'll call it bank transaction as part of this lambda function. Again, we could call it dog poop if we want it to be called X called Zed or whatever variable. But again, it's a bank transaction. So call it bank transaction, lambda arrow to the right of the lambda arrow, what are we doing? Well, again, dot map, takes an object bank transaction, and returns a specific like a subset of its properties, or, well, a single property, typically, in the real world use a single property. To return a subset of its properties, you'd need to have like a DTO, or something, but we won't go there. In this, in this example, we're just using a single property. And that's pretty much most of the time what you see with this operation. So we're gonna say bank transaction dot get amount. Okay, so just to reiterate, we had a filter, and then we have dot map, bank transaction on the left, lambda in the middle, bank transaction dot get amount on the right. So we're just going to say, hey, take the input of a, an actual bank transaction object and sort of coming in and spit out an amount as the output. Remember, the backing functional interface for dot map is function, function, input type T, output type R, okay, T R or generics. T in this case is bank transaction R in this case will be double, y is the output type double because we are telling it bank transaction dot get amount, what is the data type of get amount, the data type or the get amount data type, what's returned from that is of type double, because the amount is of type, double input type, bank transaction, output type double. So we've just translated if you will, in our stream, I like to say I like to use the expression upstream. In other words above this operation that we just did. I don't know if that's actually a thing, what they say makes a lot of sense if they call it upstream. But anyway, I say upstream. So upstream from the dot map operation, we were receiving bank transaction objects. But now downstream from the dot map operation, we've just changed that into double data types. So above map, we had bank transaction below map now downstream, we're going to have double. So that changes things. Alright, because we're no longer working with bank transaction as an object type anymore. We're now dealing with double. Okay, we have just mapped it to be something different, we've changed it, we transformed it, it's almost like dot transform, have you better name the dot map. But anyway, we've transformed it from one input to a different output from bank transaction as an input to double as the output. So now downstream below, with the next operation, we're dealing with double doubles. Now we're no longer dealing with bank transaction. As a type, we're dealing with the the amounts that belong to those bank transactions, downstream below dot map after dot map happens. So after the dot map, operation happens, picture each of the bank transactions for last seven days going in, think of your bank account and all that information you see on the screen, the last seven days, all the transactions, you know, credits and debits and from, you know, this grocery store, and here's your paycheck. And here's like all these transactions, all the details, you know, the dates that they happened, the name of the retailer, or whatever, the as well as the amounts in or out and all like there's all this information, but then we when we do dot map, which is like transforming it. Now just picture yourself zooming the screen in, specifically on the amount.

28:55
Column, all we're looking at now is the amount, okay, there's still a bunch of them still a bunch of amounts, and they are from all of those transactions on the screen, you're just throwing away all the other data around the screen, and you're just zooming in to the amount. That's what we're doing. We're zooming in on one property, one piece of data amount. We're throwing everything else away, so to speak. Okay, so we haven't changed anything, we haven't changed the underlying transactions at all, that the bank transaction objects are still sort of there. It's still those objects. But now all we can work with is just the amount in other words, just the double data types for amounts for those transactions. Okay, so that's important understand, downstream after we do the dot map, we're transforming it from one thing into something completely different. Okay. And that's where students often get mixed up. And they think, Oh, when I do a dot map, all of a sudden now I've lost my object. I don't know where the whole object went. All I have is like a piece of the object and I say exactly That's what you're telling it to do. That's what dot map does. You're, you're sort of throwing away the rest of the data. You're just zooming in on that one property. Okay? Cool. Let me take a sip. So hopefully I've over explained that hopefully you make it makes some sense to you, you and your mind. But remember, I should say, remember, let me tell you, what is dot map return. Remember, dot filter returns a stream of the object that we're iterating over right? The stream of Well, in this case, it was the stream of bank transaction objects. But what is the dot map return? Any guesses as to what dot map returns? Again, the two hints are it's an intermediary operation. And it underlying it, it has the function with T as an input and R as an output. So because dot map is an intermediary operation, it returns another stream. Okay, that's what intermediary operations do they just return another stream of something? So because it's an intermediary step, we have a stream of something. But what is the of something? What is the something I should say? Right, the something is now different, we don't have the object anymore, the Shinsei object, we don't have the original object, which is bank transaction. Remember, the underlying functional interface here is a function which takes a type T the bank transaction, and the output type OMA output type R, which in this case is double. Remember, we're doing an amount we're outputting an amount bank transaction dot get amount. So because we're doing get amount and amount as a double, the output now is a stream of double objects. It's no longer a stream of bank transaction objects, we have mapped it, we have transformed it to be a stream of double objects. And that has, I guess, consequences. I don't I don't wanna say consequences that has implications. Okay, we are no longer dealing with the bank transaction objects, we are now dealing with a native data type to Java, Java knows doubles. And because Java knows doubles, we can do something with that. Okay, there's special operations that we can do with double. So let's talk about that. What can we do with it? Well, the next step, we're going to do a terminating operation, a terminal operation, we're going to do dot collect. So we're going to collect all of those double amounts, right, all those bank transaction amounts, not all those bank transactions, are collecting all the bank transaction amounts that were from the last seven days, that's what this these steps have done. We filter get the last seven days we map, turn it from a drip bank transaction into the bank transit transactions amount, and then collect it. So we're collecting the last seven days worth of bank transaction amounts. Okay, what are we doing with collect? Well, we're gonna do something special ladies, gentlemen, we're going to do collectors dot, summarizing double. Now this I'm pulling out of my butt, this I did have to Google. But there's a special collector called summarizing. And then there's a bunch of different summarizing operations, you can do summarizing int, I think you can do you can do summarizing in summarizing double and summarizing the long. Okay, so this is a, this is a method that we're invoking. And you can pass in the amount so it just takes in summarizing double takes in

33:37
a what's called a to double function, but it's just like a function where you pass in a mount. So in other words, the summarising double just, you pass it into the mount and then you just on the on the left of the lambda and on the right of lambda, you just have a mount, you're not going to change it, you're not doing anything crazy with it, I just have a MT arrow amount, which looks kind of weird, but that's what you're doing. You're just saying don't do anything special with it, just take the double that's coming in from upstream from dot map, and just use it right. So summarizing double, returns something special, it returns something called Double summary statistics object. And this double summary statistics object which is returned its as opposed to collecting it to like a list. In the past example we said collectors.to list to take everything and turn it into a list. Now we're turning the bank transaction amounts into something called a double summary statistics object. Why are we doing this? Well, because the double summary statistics object which I just called the summary statistics object, gives us access to some really cool methods we can do dot get average get max get min get some we can do and then to do more, we can do a bunch of stuff with it. Okay. So, the the the important things to note there is that You can do get average get max get min get some average max min, some. Okay, those are for Super you oh and count. So I forgot count, average max min sum count. That's what this thing this summary statistics thing does for us. Okay, it unlocks a bunch of cool mathematical stuff for us to work with. Okay, so that's what the summary Summary Statistics does. And we were able to do that because we did the dot map step where we returned a double amount from the list of transactions that we're dealing with. And then we collected it into something called summarizing double what we use, collectors dot summarizing double, which returns a double summary statistics object. I know there's a lot of words going around there. But the point is, we can have this summary statistics summary statistics object and say dot get some, in other words, we can return dot get some. And that's it, we've now collected the sum of bank transactions, the bank transaction amounts from the last seven days, we return the sum, and we're done. Now the beauty with with this is we could also return the min and the max, the average and the count. If for some reason those were helpful, we could have done those as well imagine the old way what you would the work that you'd have to go through and the code that you have to type out to get the min, the max, the average sum and the count, right, that would be like that would take you all day, potentially, I don't know who knows. And now we just have it all in this one beautiful little operation called collectors dot summarizing double. So it's beautiful. No, just kiss to this. Streams stuff brought to you by dot map, being able to take an input type and trance, transform it into a different output type. Okay, that's what dot map does, again, not a great name, I prefer dot transform. I don't know what a better name would be then than that. But anyway, nothing to do with a hash map. That's something completely different. This transforms inputs into a different output. So that downstream, you can do something with it, like we saw here. Now when we take we zoom in on the bank transaction amounts,

37:14
we can do something with that. Now. caveat to everything I just said, you could probably get away with not using dot map, you could probably just done collectors dot summarizing double the input would be bank transaction, and the output probably could have been bank transaction get amount. So I'm kind of cheating here because I wanted to show you the dot map and explain dot map step. But that's a great way to explain the dot map step just goes to show you can make this five lines of code instead of six if you really wanted to stream to the best. That's me dunking on the old way of for loops, and while loops. And I hope you've learned something valuable here today. So hopefully, this was fun. Again, if you're listening at any realm close to 2021 and 2022. And you are a fairly new beginner to this whole coding thing. And your end goal is eventually to get a job as a coder, I really strongly suggest you check out our brand new Java fundamentals core Java boot camp that I just introduced as of today, as of the recording of this lesson or this episode. coderscampus.com/beginner will get you into you don't even have to like apply for it. Okay, there's no application process. We are accepting everyone. Okay, so Well, I should say so long as you have about 20 hours a week. Maybe we shouldn't accept everyone if you don't feel if you can only put in like four hours a week here. Yeah, then you're not committed. So yeah, you should have 20 hours a week to get to this. But as of right now, the recording we actually don't have an application process. So yeah, join coderscampus.com/beginner. And I look forward to seeing you on the inside of this 16 week, Java or core Java coding boot camp where you are going to learn the fundamentals of the Java language and you are going to do for crazy hard challenging assignments. For someone who's a beginner, you're going to get four beautiful code reviews, explaining everything that you've done, right, everything needs improvement, any conventions that you're not following, picking up on any bad habits and correcting them right there. And then it's beautiful process. With this, the code reviews are the best, I love them. Plus, you're also gonna be able to hang out possibly with me three days a week, most likely with one of my lead instructors. But hey, I often pop into those calls every once in a while and say hi to my coders and private Slack channel, you'll be able to get access to me there pretty much every single day seven days a week. As long as I'm awake and my wife is not within earshot or when I look I will respond to messages unless I need to be a dad and do family time then I will put my phone down but other than that I am responding to Slack messages and helping you guys out to achieve your goal of ultimately getting a job as a coder. So this is this is the best place to start if you are a beginner coderscampus.com/beginner Check it out. I'm super excited about it 25% off today until Monday, all was it November. What is Monday, November 29. My Calendar. Yeah, until the end of day, Monday, November 29, which is pretty pretty much like the morning of Tuesday, November 30 2021, you can get 25% off. But like I said, even if you don't get that discount, before time expires, 100% of what you've paid will go as a credit towards the next bootcamp, which is the full bootcamp which is the one that guarantees you that job. Okay, so it's pretty much like you're getting a free boot camp for 16 weeks, and you get to hang out with cool, cool people, just like yourself, and hey, me, too. So hopefully, I will see you there. I think it's one of the most amazing deals I've ever put out. And I'm truly proud to say that I'm able to release this bootcamp. So can we see on the inside coderscampus.com/beginner, and I can't wait to see you in the next episode. Take care of yourself. Happy learning. And bye for now.

41:03
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 EP54 – What is the Map Operation in Java Streams? appeared first on Coders Campus.

  continue reading

64 פרקים

כל הפרקים

×
 
Loading …

ברוכים הבאים אל Player FM!

Player FM סורק את האינטרנט עבור פודקאסטים באיכות גבוהה בשבילכם כדי שתהנו מהם כרגע. זה יישום הפודקאסט הטוב ביותר והוא עובד על אנדרואיד, iPhone ואינטרנט. הירשמו לסנכרון מנויים במכשירים שונים.

 

מדריך עזר מהיר