Stream API in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign API in fact in the earlier video we have talked about for each and I hope you got some understanding of how to use forage and in this video as well somewhere we'll talk about it but important topic for this video is stream API now think about this when you talk about collection so why do we use collection in Java to store data right of course if you have a lot of data you can store that in a collection it can be normal primitive values in fact in collection we don't say primitive value we save the integers of float or double in the format of their classes right or the objects so basically we have wrapper classes so let's say if you want to store numbers if you want to store string of course you can mix data but let's be very specific to data here it can be integers numbers or it can be complex objects let's say I want to create a collection of laptops which will have laptop ID laptop name or something like that the idea here is in the collection you can store data now sometimes you want to process that data so how do you process data of course you can use a normal for Loop or you can use external for Loop or you can use for each to get the data and you can print the only thing is sometimes you need to process the data in terms of changing values sometimes you have to change data in terms of filtering the values and then sometimes you need to reduce the value so this is the option of stream API here which creates a stream of the values which you have and then you can do the operations I know a lot of things are looking theoretical now but let me just jump to the code here itself so in the last example we have done this right we have list of values and I can perform some operations on it so what I can do is at this point uh let me just remove this extra code from here now if you have not seen the earlier video Don't Worry what you have is you have a list here which has some values which is uh let's say five integer values and then I was able to print them with the help of for each now what I will do is I will perform an operation where I want to map this value and double it so example whatever value I have here example four I want to make it eight okay so I don't want to change the existing list because that will make it mutable I normally you know when you want to work with a lot of data and when you want to use multiple threads it's always better to have immutable data which means don't change the existing value if you want to process you can do it in a new stream okay what I'm saying is whatever I do with the values will not affect my least as simple as that so what I can do is I can create a stream here of values and I want to print the double of it so I want to say eight uh 10 14 16 and 18. so the way you can do that is you can create a stream out of it now how do we do that it's very simple you can say nums dot uh you can see we get different methods here we have talked about for each and if you are familiar with collection API uh you might be doing add get and all those methods but in Java 8 we got this concept called stream and you can use this stream and if You observe when I typed stream here see if I say stream we get one more option of parallel stream that's right so if you have huge amount of data and if you want to do the processing using multiple threads you can do that and the beauty is you don't you have to create the threads by yourself Java will handle for you at this point I don't want to use parallel stream let's go with the stream and what this stream will do so if I jump to the stream method which then the collection you can see it's in collection the stream method returns you a stream okay that's weird right so this this is a method this is a type okay so if I jump here this is basically an interface stream which is extending based into base stream which has a lot of methods to work with at this point we want to use one of the method and if you remember at the start of the video I mentioned we want to change the values right so I want to make 4 8 I want to make 5 10 so that can be done with the help of map so looking at the syntax things look difficult let me just simplify that for you I will go to demo.java and here I want to create a stream that's done but then when you call this stream it will return you the stream of values so what I will do is I will store that in a stream which has integers and mind you you can also use different complex data here not just normal values okay maybe I have to import the package yes and then I want to give a name to it as well so I will say this is my data of course you can call it anything but I'm saying data so basically when you call this method stream it will return you this stream of values and what that's what we are storing in data now with this data of course instead of printing nums now I can print data as well so you can see I got this stream which is data here and on this data I am using a for each method so what this forage will do is it will pick up the data from stream one by one and it will print that's what we are doing here just printing the data okay and if you still want to understand this in the proper way you can watch the earlier video of forage this will make much more sense and now after doing that let me just run this code you can see we got the same values right so then you will say hey what's the advantage of using stream here see the thing is we are not working with the existing list we got a stream here on which you are performing the operation now in future even if you change the values of the stream it will not affect the list that's one the second one is once you use the stream which means once you consume this stream you can't reuse it okay I will show you that in some time and by the name stream what it means stream of data right when you have multiple data like we have the flow of water stream of values right okay so what I was saying is once you consume the stream you can't reuse it let me show you the proof what I will do is see at this point this stream has been used right it has been consumed and if I try to do this once more okay I'm doing the same thing again I'm using the same data for footage and the moment I run this code it will first print the first frame you can see this line is no it has no issue line number 14 is working but the moment I run the line number 15 this is what it says it says uh exception because of this stream has already been operated a bound or closed that's why the moment you use stream once you can't reuse it and that's the beauty see for the beginners and if you are learning for this for the first time it will not make any sense why you have to close something when you can use it multiple times but you when you work on the project when you have huge amount of data it's always better to work on the data and close it so that you will not get data leakage or you will not have some unused resources open Okay so this will not work so Point remember you can use the stream only once okay another thing we want to do here is I want to perform the operation which is I want to map I want to map this values the way you can do that is in fact you know before that I just want to check do we have any other method which we can use at this point uh you can see we have four each we also have count okay so what count will give you is the size of your stream should we try this first let's try so I'm saying count now and again once you use a stream at this point when I say data dot count the stream has been consumed or it has been operated on and if I can store that in a long variable long I will say count itself so long count equal to data dot count it will store the values uh the the size of your stream and basically you can print it as well you can print the count so you can see if I print count here this will work okay so we are printing the count value as well and what do you think now let me in the comment section if I run this code will it work properly of course it will print the count but do you what do you think it will work perfectly or you will get an exception let's try so let's run this code and you can see the we got the value 5 which is the count and also we got the exception the same exception why is because at this point the stream has been consumed and here we are we are trying to consume the stream once again and it will not work make sense right and now of course this will not work so I can just simply comment this part so you can see we got a count and this is working apart from this we have multiple methods right so let's not use this now let's we don't want to use count I can simply go with data dot uh what other methods we have we can use count we can use map uh find first you can also apply the filter to find First and those things just to keep it simple okay we also got sorted here which will sort the values for you and you can see this sorted actually returns a value sorted returns a new stream okay so that means if I run this sort it will basically create a new stream and I can say stream of integers and of course I have to give a different name or maybe I can use the same variable that works but let me just have a different value so I can say sorted data now this data is okay so you can say this is another stream and this is a new stream now yes the earlier stream has been consumed here but you can use the new stream here so what I can do is I can run I can print this value but again I can't use data as you can see if I run this code it will say the stream has already been consumed or operated on so we have to work with the new stream which is assorted data so this is the new stream which we want which we can act upon and if I run this code uh you can say it will sort the value and the beauty is the values were already sorted okay so let's say six and let's say to let's say eight eight is good and this is let's say one okay so now I want to sort this if I run this code and you can see we got sorted values so stream provides a lot of different methods to work on that data you can solve the values uh you can print the values but then we want to do one important thing right which we've talked about we want to double the values and print so what I will do is even before I use a stream let's try this with normal Loop how do you double a value and print so what you do is you use a normal for Loop or maybe you can use the enhanced for Loop so I can say int n colon and I can print I can fetch the value from num and then once you get the value you can simply double the value and print I can create a new list to store the values or I can just print it here itself it makes sense to create a new list and do that and if you run this code you can say it will double the value and print that is working but again this is the external Loop and you are working on the data so better than this in stream API we got another option I will come in this part and go back to my stream and I don't want to use sorted data anymore let me just work with the data itself but what I want to do is all this data I want to double the values so the way you can do that is by saying data dot so you can see we have a method here called map okay and if I say map you can simply pass the value here and you can perform the operation whatever opposition you perform maybe you want to double the value maybe you want to subtract the value so whatever operation you want you can do map it will simply say hey I will give you the value you tell me what to do with that value so what I want to do is I want to double the value right so it will simply give the value I can store that value in n and then on that end what you want to do I want to double it that's it this is your map and even map if you can go to map map itself gives you a new stream so what I can do is I can say stream integer and I can say mapped data so map data equal to data dot map and we are performing performing an operation so at this point this is your new Stream So what I will do is I will just print the new stream I will say mapped data and then I'm saying for each let's run this code and you can see we got double the values again the same thing which we have done here but if you can see this Logics makes much more sense so when you read the code you will actually understand what is happening right of course you you need to get used to a Lambda expression and stream API but once you are used to it you can actually read okay so at this point we are getting a new stream we are mapping the values we are doubling doubling it and then we are printing it right you know the words of beauty is at this point this is a new stream right so we know that nums.stream actually creates a stream and then on that stream you are applying map right so can we do this I will say dot stream dot map here itself and I will say n and n into two so basically what I'm doing is whatever we are doing on this line is now already done on the new on the upper line done so we can do that in one line and we can also run this for each I will just cut this part and paste it here so you can do everything on the same line the only thing is when you are printing the value here itself because the forage will not return the value right so this will not work now so even why do we have to use a stream now I mean why we have to even refer that when you can directly print it so what I'm doing is I got the nums which is the list on that list I'm applying a stream operation which says create a stream for me on that new stream I'm applying a map which will double the values and it will create a new stream and on that new stream we are running a forage so this is if you know design patterns we this is some something called Builder pattern where you build a new stream with every everything and you know one of the best way to write this is you can say numbers.stream I mean just for the visual representation you can just say dot dot or new line it makes it much more readable so you got a new stream from this list and then you are applying a map and then you are printing it this makes sense right and now if you run this code you can see we got the same value in fact you can do one more thing before mapping you can actually say sorted so what we're doing is okay first of all let me know in the comment section with the times time how many streams we are creating with this code let me in the chat window oh sorry in the comment section so let me give you the answer this is where we are react so one stream second stream third stream so in total we got three streams here with this three lines now you will be saying here we are wasting so much of memory uh not exactly the moment you completed the first stream it is gone now right you've got a new Stream So basically we are replacing we are not duplicating it so this is how it works you can see we got the values it is in sorted format in fact you can do one more thing maybe you don't want to double all the values you just want to double or maybe you just want to sort the values which are odd numbers do we how many odd numbers we have here we got one and two let me just add one more odd number here which is seven so I want to perform this operation so at this point if I don't it will apply on all the values I want to do this only on let's say odd numbers so what you can do is before the sort itself you can say dot and there is option called filter at this point if it is confusing you what this map is doing how exactly it takes the value and perform the operation we'll see that in some time or maybe in the other video uh so in this filter what filter will do is filter will take the value from you example it will say uh and whatever value you want to pass and then on that end on that filter n whichever value it will pass we have to apply the filter now how do you filter so I want to filter based on the even odd values so I want to perform this operation only when the value is odd so when you create a new stream it we can pass the values which are only odd numbers okay make sense so now I what we do is we have to say I want to filter based on 2 equal to equal to 1. so if they are odd numbers this is how you find even odd right so this is how you find the odd number if this is true it will pass the value ahead it will say sort and it will say map it will say for each and now if you have this code you can see we are only finding so we got 2 because we got 1 here double the value of 1 is 2 then we got five uh 10 and then we got 7 14. this is working now I know you have this confusion how all these methods are working okay for this let's do in detail so let's let's work with one by one let's talk about filter now when we talk about filter if you jump to filter what filter filter does filter has this type or the it takes an object of predicate now what is a predicate so predicate is a functional interface which has a method called test and this test returns true or false right don't you think that's what we are doing here this will turn true or false and we are filtering based on that but what if we don't want to use Lambda expression what if you want to use a object of predicate so let me help you with that so what I will do is I will create an object of predicate so I will say predicate and predicate will be applicable on integers and here I will say let's say test itself or they can say ready equal to and then I can create the object office against a new predicate but this predicate has a method called test if you can see and we have seen that right in predicate we have this test method so what we are doing is we are creating an anonymous inner class and in this you can pass a value so let's say let's say if I say n a short integer let's say n and based on the test you want to do so what test I'm going to do I want to test this if this n mod 2 is equal to equal to 0 in this case return in fact I want to test for odd numbers right 1 in this case return true otherwise it will return false I can say else return false okay this works so basically you can see I am creating an object of predicate here basically I'm trying to instantiate it with the with the help of Anonymous in a class which is this and then we have set the condition as well and we know that filter takes an object of operated so I can simply say predict this works let me just run this code and you can see we got the same output now what I do is uh we have seen Lambda expression right and we know that when you have a functional interface and when you have this syntax here from here to here it's of no use and we can also remove this currently packets here so we can remove the outer curly brackets let's do that remove remove okay that's done in fact you know before removing I want to do one more thing I wanted to show you one more thing which is don't you take a show of using IF else I can simply say return n mod 2 equal to equal 1. see this itself returns you a true and false value right because this is double equal to and then this method just returns a Boolean value so why we have to even write the true and false okay number of lines have been saved okay and now I can literally remove this part so remove this part from till here to here in fact not there till this point and remove this uh curly packets as well and then we have talked about it we just have to put an arrow here again Lambda expression nothing new one more thing when you have only one statement you don't need to put curly brackets let's remove that if you have only one statement and even that one statement is return then you don't have to put or you don't need written there it will give you anyway it will give you a so we don't have to put return you can remove return and also this integer type is optional so you can remove that and this round brackets are also optional you can remove that so you can see uh from this big chunk of code we were able to remove everything because of Lambda expression and again I have made a video on this you can just go back to the previous video and watch and now so you can see if anyone you have ready can you put this code instead of pretty here you can right if you can use if a is equal to B if you whenever you have a you can use B there right so that's for filter uh can we do it for map for map also we can do the same thing um but I want you to try that on your machine so go to map and find which method which type of what type of object it takes you can say takes function and if I go to function function is again a functional interface Lambda expression we can use it and we have only one method called apply right so we can use that and try this out and let me know in the comment section you're able to do that or not so that I can make another video now before I conclude with this video I want to do one more thing what if I don't want to print the values you can see we got to 10 14. what I want to do is I want to add all these values and print so example it should be 2 plus 12 12 and 14 is uh 26. so I want the output to be 26 not individual values so basically what you're doing is you got the values you applied the filter then you tried to map the values and then now you want to reduce Now by any chance if you're coming from the world of big data we have this concept of mapreduce there which filters the data which Maps the data and create a reduced part of it so we are trying to do the same thing here so instead of 4 H which brings the value can we just reduce the value and that's why we have this method called reduce if you can see now say if anybody you add three values how do you do it so let's say if I give you two value three values 2 10 and 14 in your mind how will you do it think about this so if you got three values first of all you will add the first two values you will say Okay 2 plus 10 is 12 and then the output of the first addition will be added with the last value which is 14. so you will say 2 plus 10 is 12 12 plus 14 is 26 right the same thing that we do here but the only thing is when I'm saying we have to do the 10 initially can I say 0 plus 2 is 2 then 2 plus 10 is 12 then 12 plus 14 is 26. so at this start we have to start with zero so I can say for any value n take the value and perform the operation I don't know why I forgot the syntax is it okay I literally forgot the syntax of reduce what I will do is and that's part of normal Java life if I go to reduce reduce takes how many parameters it takes an object it okay it takes two but two parameters first it takes the value okay which is T in fact I will jump to this method here so you can see it takes two values one is the value itself and second it takes the object of binary operator okay which means basically first you have to pass the value n and then we have to take the object okay this binary takes a value okay I guess it's just by function yeah so there's the apply method which takes two values so basically we have to start with zero and then it takes two values one is let's say I will say C comma e and then I will perform the operation which is C plus e now you will be saying why this named c and e uh remember when we do basic mathematics when you add two values so example if I say the two values are 99 plus 88 so what you normally do is you say 99 plus 88 and then you try to add this value to say 9 plus 8 is 17 so you will write 7 here and then 1 will be carried here right so you say carry oh basic maths I love it so in the same way you can do you can say this is ease element whatever element you have passing example when you say 2 plus 10 so this is 2 this is 10 and you can add those values here and this 0 is basically a starting value from where you want to start and I just hope this will work now the only thing is with this reduce if you can see it returns a value so basically here I need to accept the value I would say ain't result is equal to so whatever basically you are reducing it will return the value and then the same value I can print it here it's that simple let's run this code and you've got 26. so the question is what is this zero remember as I mentioned when you add these three values which is 2 uh 10 14 initially you will say 2 plus 0 so that is that 0 here and then whatever the output of the first operation uh will be C which which is the carry and the new element so when I say 2 plus 10 which is 12 so 12 is C now and E is your new value and that's how we can use stream API in fact you can also use parallel stream the only thing is sometimes your values are dependent on the earlier operation in that case don't use parallel parallel stream because it will do the operation parallely at this point I guess this sorted is only the blockage example if I use parallel stream here I think it will create an issue because of the sorted here let's try okay it is not getting an issue anyway why do we even need sorted here so I find this code now uh I don't know if it is faster or not but parallel stream basically use multiple threads to do this so it all depends upon what type of data you have whatever operation you want to perform parallel stream will make sense so yeah that's it from this video I hope you enjoyed about stream API which is a new include in Java 8 and if you want more such videos on stream API features let me know in the comment section and do subscribe for the videos bye bye everyone
Info
Channel: Telusko
Views: 55,103
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: tklkyVa7KZo
Channel Id: undefined
Length: 26min 3sec (1563 seconds)
Published: Wed Oct 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.