Array Method filter map reduce

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay so in the last video we have seen how do we use the method which is for each works for arie right and then we mentioned that we have some more methods so let's explore those methods here but before that i just want to do one thing i just want to remove the extra two parameters which we used uh let's keep it simple all right so let's stick to this one so yeah we are passing only one variable and then we are printing it that's it and let's rerun with that particular thing yeah it is working okay so what else we want here see i want to print all the values right that's what we have done and now we have another requirement so the requirement is how do we print even numbers it's very simple actually once you got the value right which is n in this case now this n will be replacing i mean this n will be having the value of 42 51 24 1 by one right so this is a callback function which will be getting called every time you have a new value that makes sense right but then it will be getting called six times for every value it will be getting called and in this particular function we can write the logic and we can use if else here we can say if the value is divided by two if it is divided is 0 that means it's a 1 number we can write that logic inside the method itself or the function itself the problem here is we will be calling this function 6 times for every value now you might be saying that's okay right we are just printing the value right we are not doing much yes that's true but what if this is a function which is very loaded there are so many statements there are so many variables there so many operations are going on and this is not just only six values but if you have thousand values so imagine for a thousand times you're calling this function and of course not every called will be useful because we want to just use only for even numbers right and so i don't think this is a good idea right so what we can do is before going for forage before printing the value we should be able to filter these values and that's right we have a method called filter in array how that works so what we'll do is before forage we will first filter the values so we'll say nums dot filter okay now this filter will take one parameter same as for each so it will take one parameter let's say n again of course this two ends are two different variables okay this n is local for the callback function of filter which is your passing here and this is a local variable for the callback function which you are using for for each two different functions so we can use the same variable so let me use the error function here now what we are doing here is we are saying it will pass a value the only thing we have to check is actually if this value uh actually if we want to return the value which is true or false now since we only have one statement you don't have to maintain return keyword but written is there okay if we are returning true or false filter will allow the value only if it is true so do we check so for this particular n i want to check if it is even or not we have seen that if n mod 2 is equal to equal to 0 triple equal to 0 if this is true that means it will pass true for each value okay initially the value of 42 is it even yes it will return true 51 it will return false 24 it will return true so for every true and false it will send that value ahead so what i'm saying is if you try to print this i'm not sure if you can really print these things let me try i've never tried this before let me comment this section uh you know java trick is is actually a very tricky language to work on so let me print and let's see what happens okay so i'm trying to print this on the console and this is what we got okay so you you can see we got all the even numbers so filter basically collects all the values and then it will return that as an array itself so the output of this is an array and we have filtered it now you can perform filter by anything you want maybe a single operation like this maybe a complex operation the only thing is if you have multiple steps you need to open the curly brackets right you have to open the curly brackets here and close it and you can do whatever you want there maybe ten lines of code hundred lines of code thousand lines of code that's your choice it just needs true or false okay now once you've got a filter let me just go back to my earlier code yeah now once you've got a filter on the same values which is here after the filtering you can simply apply the for each and that's right we can do this i know that sometimes it looks weird but it works so basically what we're doing is first you got nums which is an array of six values on that you are applying a filter which will only return four values and then on that four values that you are just printing it and that's it it's so simple right and let's see if that works and yeah it works you can see we got all the values one by one so this is working now what next we can do so the next thing is we have done with the filter now it's time to do something else what we can do is for these filtered values i want to double them so this 42 will be let's say 84 24 will be 48 so i want to double all the values how do we do that uh of course we can just in the for each we can just say multiply by two our job is done right that should be simple and you can see we got all double values but sometime you it can be a complex operation and maybe you don't want to do for each you you just want an array with double the value in that case you can apply another method which is map so map what will do is it will take a value it will change the value that's why it's a map okay so take a value change it something maybe double the value maybe subtract it maybe perform some complex operation maybe save somewhere it doesn't matter we just need to map it so in this map it will take one parameter which is let's say n again again as i mentioned this all these ends are different variables with the same name but it works you can have a different variable or same name variable it doesn't matter now on this map what you want to do is you just want to multiply by two that's how you double it so take a value and double it so filter it will remove it will filter the elements based on the condition we are saying only even numbers it works now on that even numbers the numbers will go ahead and then on that even numbers you are applying a map so you are taking each value and you are doubling it with two so if it is uh 42 it will be 84. and once you are doubling it you are just printing it i mean though this the same array goes for the for each and you are printing all the values let's try that works and yes it it works you can see that simple right so filter basically takes a parameter or takes the argument it will check for the condition if that is true it will send that value ahead now map will simply take a value and double it and for it will take the value and print it that's what we have written there right cool so these are the three methods in fact we can use one more what if i don't want all the values here what i want is i want the addition of all these values 84 48 196 24 how do we do the addition see after addition you will get one value right so basically what you're doing is you're you're reducing the value and that's where we have this very famous method called reduce so we'll not be using for each because for it will be applicable when you have multiple values reduce will give you only one value so why do we need forage so instead of foliage will use a reduce method i don't know if you have heard about mapreduce if you don't know about it just google map reduced by google they have written the theory of it or the paper okay so mapreduce map and reduce first you will map it and then you will reduce it to one single value now what we are trying to achieve here is we want to add all the values how do we do that uh so basically when you want to add something at one point see even if you want to add 42 51 24 you have to take two values at a time right you can't simply add a 42 with which what value you will add it so you will take two values 4251 the addition will be added with 24 and then that edition will be added with 98 or whatever the value is so basically we need two parameters right so we'll take a and b you can see that we are using different variables this time now with this a b you've got two values right you want to perform some operation and the operation we want to perform here is a plus b we want to add the value that's it and then whatever the addition is stored here uh you will just save that somewhere that was a result so because this videos will return one value right and you're saving that result and here i'll just try to print result i'm not sure if this works let's try oh it works we're not even sure if the right answer maybe you can use a calculator to check that this is uh 1 32 and it looks the right answer right again you can check that out and let me know in the comment section if this is wrong okay or maybe we can just use a simple value just to calculate it just faster so we got how many even numbers let's take three even numbers so 2 4 and 6 2 4 and 6 is 12 24 the answer should be 25 if this one this code and that works so this is how you reduce it uh okay so this is how it works so we got filter map reduce and we also seen foliage so i hope you enjoyed this video of different other methods and that's it thank you so much for watching bye [Music]
Info
Channel: Telusko
Views: 52,371
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: oQ7Le6SuLNA
Channel Id: undefined
Length: 9min 34sec (574 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.