Java Streams Tutorial | 2020

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi are you still writing code like this in 2019 well if you are then you must watch this video hi my name is Nelson and in this video I'm gonna show you exactly how to use Javas trainers we all wrote code like this in the past and to be honest nowadays this is pretty much yeah just just bad really because we are moving towards writing functional programming and when we write code like this it means that we have to specify every single implementation or every single part or the actual moving parts of the actual code and this can lead to a lot of errors bugs and pretty much you know no one does it anymore so in this video I'm going to show you exactly how to use Java streams which allows you to move away from imperative programming to declarative programming without further ado let's dive into it alright let's go ahead and learn about Java streams if you grab the source code for this video go ahead and check the link down below I've got a github repository with all the examples that I'm about to cover so basically I've got this project right here where I've got a class called person so if I just shift out for a second so I've got this class called person that has name age and gender and then I also have this enum right here which is the actual gender so male or female and then I've got this class right here which is the main class and this is where we're going to write every single piece of code so let's go ahead and learn first what I mean by imperative approach so with imperative approach you define every single step for what you're trying to achieve so let's say that I have this list of people right here so let's say that we want to filter down every single female in this list right here so with imperative approach what you would do is something like this so you would start with a list and you would say this is person and then you'd say females equals two and then Neil ArrayList and then what you would do you would do something like this so you'd say four and then person and then person and then this would be in people and then you would have an if condition so if and then person dot and then get gender dot equals two female you would simply add it to your list so females dot and then add and then person right so finally let's go ahead and simply bring this to the console so let's go ahead and loop again so simply go ahead and say females dot and then for each and right here within fancy consumer system dot out and then column column print line now if I run this you can see that we only have female in this list so this list right here called female right but you see that right here we are defining every single step of what we're trying to achieve so first we define a list and then we loop through the list and then we perform an if condition right here and then we print so what Java streams allows us to do and by the way Java streams works really really well with collections so it's made for collections so what streams allows us to do is to simply ask what we want so instead of us defining you know this implementation right here we can simply tell it what we want from our collection so to give an example so with declarative approach we have few methods within this stream API basically we have filters sort or match any non match man makes group flatmap so on and so forth but in this video my lecture exactly you know these ones here and if you don't learn more about Java streams go ahead and check out my course on Java streams which is absolutely free so what I'm going to do is the exact same thing right here but using Java streams which is using a declarative approach so we have few methods out ourselves so filter let's go ahead and start with filter so to use streams and filter what you have to do is simply say people so this is the initial collection that we want to start off and then simply say dot and then stream so what do you say stream this takes you to the abstraction and from now on you simply ask what you want from the list so right here we have a method called dot and then filter and filter takes a predicate and a predicate simply returns true or false and this is what you want to keep from the original list so in our case this will take a person right and then we have two before me conditions so our condition was person dot and then get gender dot and then equals to female right so now that we perform the filter operation now we have to collect the results back into a list and what this is doing is giving us a brand new list so to collect to a list simply say dot and then collect and then say collectors and then to and then list just like that so you can see that now I can extract this to it variable and what I want to say is females and in fact let me go ahead and comment in this code and that there there we go so now I can grab this same line and then paste it here and if I run the code you can see that we have the exact same thing but right here you see that we know you know creating a brand new list and then looping through that and then performing the if condition and then if that said and then if that satisfies then we add to the list which you know it's too much code for something very simple so this is when the clarity of approach comes into play so we can do other things other than filter so if I go ahead and pretty much just comment that line and we can also sort this list right here so the initial list right here so people so let's say that we want to sort the list actually first let me go ahead and simply loop through people so if I uncomment that and then run this you can see that we have both male and female but the actual data is not sorted so you can see that first we have James born age twenty thirty three which is Alena fifty-seven 1499 seven and and then the 120 but let's say that we want to sort this list by the actual age so to do that what we can do we can simply say people dot and then stream so remember every time that you want to use the clarity of approach and enter the abstraction mode where you pretty much just ask where you want you see we have to say don't stream on a collection and then right here simply say dot and then sort and sort takes a comparator so the comparator goes like this simply say comparator dot comparing and then right here we can pass the actual field that we want to compare so in my case I want to compare age so I'm going to say get and then age just like that now I need to collect this two lists again so collect two lists and then if I extract this and simply say sorted and then pretty much just say sorted right here and if I run this you can see that now our data is sorted by the actual age so you can see seven fourteen twenty thirty three fifty seven ninety nine and one twenty so if you want to reverse so the actual order simply go ahead and say dot and then reverse so if I run again you can see that now it's reversed so 120 99 57 thirty three twenty fourteen and then seven and you could also change the comparators it would say dot and then then comparing so let's say that we want to compare females so females are actually gender I bad and if I run this and right here you see that we first get female and then the next one is male but then we have two females or 57:33 and then a male and then the last one which is the the youngest person in this list is a female so seven so this is awesome so we could also ask questions about our collection so this is when all match any match and none much come into play so let's say that we want to find out whether every single one in this list has an age bigger than five right so to do that we can say people dot and then stream and if you guess and then all match and this takes a predicate and right here we have to return a boolean for the condition that we are after so what we want is person dot and then get in an age bigger than five right so if I extract this to a variable or match and then South and then all match and if I run this you can see that we get true so everybody in this list has an age bigger than five so if I was about to change this to let's say eight and then run this you can see that it sauce because so Ana cook she's seven she's not bigger than eight and pretty much know everyone satisfies the condition that I've just asked so you can also check any match so any much premature just check for at least one so if I comment this we could pretty much just perform the same operation right here paste that and this will be any match and then right here instead of all match simply say any match and then pretty much we have at least one person which is bigger than a our actually more people but basically it just finds one and then returns true if I learn that you can see that's true but if I say do we have any match which is bigger than one twenty one run this this will return false because Zelda she's 120 and we haven't got someone which has an age bigger than one twenty one so also we could have the reverse knowing match so if I comment that out and then simply say people dot and then stream dot and then non match and this takes a person so now we can ask a question so let's say that we want to find out if there isn't anyone with the name of let's say Antonio so let's seem to say person dot and then get and then name dot and then equals and then Antonio and then extract that will variable and then non match if I salt not match run this you can see that it is true so there isn't anyone in this list with the name of Antonio but let's go ahead and change James Bond to Antonio run this you can see that this now returns false so as you see the powerful of streams is just you know crazy so let me go ahead and show you max and mean so basically we can ask information about the max number or the max value within a collection so let's go ahead and find the person that has the maximum age so for that people dot and then stream and then dot Max and then inside it takes a comparator so comparator comparing and you saw that before and then get age and then if I shrug this to a variable and this returns a an optional by the way and the reason why is because it might not you know find the max value within this collection right so if I pretty much just say max and right here or actually just let me remove that so what I'm gonna do is that and then right there dot and then if present and then person and simply salt and then person and we can pretty much just replace this with method reference and there we go if I run this you can see that Zelda she's the oldest person in this list so let's do the same for men so I'm gonna grab that and then paste that in and what I'm gonna do is comment that put a semi column there and then right here instead of Max simply say min run that and now you can see that and it cook she's the youngest person in our collection so this is awesome so we could also group information so this is where you want to group information based on a field that you have so let's say that we want to group this information based on gender right so for that we would expect a map with a gender and then a list of each gender and then I'll list for the person within each gender so to do that let's go ahead and simply say people dot an extreme and then dot and then collect and now we're not collecting to a list because we want to map weight the genders and then each person within that gender so now we can use collectors dot and then grouping by so grouping by takes the actual field that we want to group I and what we want to group by is the actual gender so if I then extract this to a variable you can see that now so group by and then gender so you can see that this is a map of type gender and then a list of people so let's go ahead and loop through this group gender so group by gender and then dot and then for each and this takes the gender and then people so here you could rename this to people to something else but for now let's go ahead and simply say South and then gender and now what we're gonna do is simply say people one dot and then for each and then system dot out and in column colon print line so if I now run this you can see that and in fact if I address another South there and then run this you can see that right here we have female right so this is what we grouped and then everyone within female and then male and then everyone within male so as you see streams is just so so powerful and just imagine if we had to do all of this using imperative approach right you'd have been like lots of lines of code and pretty much is just too much right so another thing that we can do is you see that I've just used a single method within string so what we can do is if I premise just comment this so the cool thing about streams is that we can chain these right so let me give you a quick example so let's say that we want to find out every single female and then grab the oldest female and then pretty much just return the first name so to do that we would do something like this so you'd say people dot and then stream and now let's go ahead and say simply filter so we want to filter females so person dot and then get and then gender dot equals to female then what we want to do we want to find the oldest female so simply say max and then comparator comparing and then get in an age now we could also use the map so let's go ahead and say map and now we want to grab the actual name and then end out with semicolon so now you can see that we have an optional so this returns an optional so oldest and then female there we go now I can say all this female age dot if present and their name and then system dot out dot println and then name and basically I can use method reference right here as well so now if I run this and now you can see that Zelda brown is the oldest female in this list so this is just insane right so as you can see you can pretty much just chain these and ask questions on your list instead of implementing every single step along the way all right so has you saw the benefits of using Java streams it's insane so the next step for you is to take out my course on Java streams where I cover everything that we covered in this video plus more go ahead and enroll to my course where I'm waiting for you thanks for watching don't forget to subscribe to get more videos like this and also make sure to follow me on my Instagram where the community is growing this is all for now join me in the next one see ya
Info
Channel: Amigoscode
Views: 94,076
Rating: 4.9578714 out of 5
Keywords: streams, java, java 8, lambda, map, reduce, filter, sort, collect, tutorial, functional, programming, how to, java11, java9, jdk, how to use java streams, how to use java collections, openjdk, lambda functions, java predicates, OpenJDK, java for windows 10
Id: Q93JsQ8vcwY
Channel Id: undefined
Length: 19min 12sec (1152 seconds)
Published: Fri Feb 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.