Hi everyone, so today let's discuss what is stream API in Java 8 and few of the methods present in stream API. So as usual let's understand from basic. So first we should know here what is stream API.
So basically stream API is used to process collection of objects. Suppose I have a list of objects and I want to process those data. Then I can go for the stream API.
or in other way we can say a stream is a sequence of object that supports various method which can be pipelined together to produce the desired result so if you observe in stream class we have few predefined inbuilt method which we can pipeline together to get the desired result okay a stream is not a data structure instead it takes input from the collections or arrays so basically we can convert collection to stream or array to stream and we can use it further stream don't change the original data structure they only provide the result as per the pipeline method so whatever the pipeline method you will use to that stream object it will get the desired result only it won't change the underlying data structure of the collection okay so here we should know why we need stream api the first advantages we can achieve functional programming means if i have a functional interface then i can represent that with lambda expression right so the second one code reduce so if you are going to write the lambda expression then the length of the code will be minimized and even as we already discussed stream up few pipeline method we can do the method chaining to reduce the length of my code then the third one is bulk operation if i have a collection with huge data and i want to perform some operation then we should go for the stream api because as compared to our traditional approach stream will give us the better performance and in this tutorial we are going to discuss these two method filter and forage so filter method we are using for conditional check like if else condition and forage method we are going to use for the iteration Even I will explain you the internal of forage and filter how it internally work. Okay, so let's get started. Let me open my eclipse.
So this is what I created a package called com.javateki.stream.demo. And here we are going to use the forage and filter method right. So this is what I have the list. Suppose we want to iterate the list in our traditional way then how we are doing it.
We are using simply the forage. and we are printing the result right but how we can write in using the stream api so for that what we need to do we need to convert that list to stream then only we can use all these pipeline method right then we can simply write the for each method so in for each method if you observe here the method argument is consumer right so if anyone not aware about this term consumer supply or predicate i would suggest you to check my previous tutorial then you can easily understand the internal of this stream api okay so let's use it and this is what the accepting the consumer right let me show you that functional interface this is what my consumer functional interface having the method void accept okay so for this method we are going to write the lambda expression inside this for each method right so let me copy the method here so as we are going to lambda expression for this particular abstract method we now need the prefix and name we can remove it and we don't need the type only we can specify the one parameter and we are going to print it right so and we need the lambda expression here then the method body as this is the one argument we know need this bracket also right so if you want we can copy this to for each method this is what the lambda representation of the accept method present in consumer functional interface we can write like this so we need to change it to the variable name t right so if you run this we will get the output we are getting the result right for this for each one and for this let me rerun again we are getting this five object which is present in my list and this for is internally using the consumer functional interface right so this is what we use for each to iterate a list suppose i have a map okay let me add here the way it will map suppose i have a map and i want to iterate this map okay so two way you can do it you can directly iterate this map using the for each okay here map have the two argument let me add that key and value then use the lambda expression here and print it so let me copy this sob statement we can simply print here key and value right so let me run this we are getting the result right the map the object entry which is present in map we are getting as a output by iterating it but here if you will use direct map dot for each you can't use the pipeline method present in the stream class so for that what we can do we need to get the entry set from map then we can convert it to stream then we can iterate it something like obj object like this we can write so if you run this we will get the result so here we understand how to iterate a list and how to iterate a map using stream api right so now let's understand how this forage method internally works right so as we already discussed forage will accept the method argument as a consumer functional interface so let me write that so that i can explain you consumer as my list contains the data type string specify the data type then create the reference and this consumer interface of the functional method called accept method for this method we are going to write the lambda expression let me write that this is the lambda we don't need the method name and prefix remove it only we need the method argument ok then we need the lambda expression then we need the method body and i want to print this ok simple logic i write here i am taking the input and i am printing it that's what internally for is doing so let me show you that consumer.accept pass the input i am giving something like java.tk okay so if i run this simply it will print the java.tk you are getting the result java.tk whatever the input i am giving to this accept method it will simply print that right so same way for each method internally doing so let me show you that what it is doing it is simply iterating our list okay then inside that iteration it is invoking consumer.accept of s1 the dynamic input so simply it is iterating a list and as it is taking the argument as consumer it is calling the consumer.accept method this is what the internal structure of for each method i will show you that with source code let me run this first we are getting the result here right so now let's go to the for each method internal source code something like list dot stream sorry for each let me go inside this yeah so if you observe this is the forage it is taking the method argument which we pass as a lambda expression then simply it is iterating a list using forage and it is calling the accept method of consumer right this is what the internal of forage and that's what i explained here okay so now we understand the forage method and its internal right so now let's discuss one more method that is the filter method so let me add here okay so what is the use of this filter method as we already discussed we are using it for conditional check right for if else kind of concept i will show you with example suppose i have a list of object and i want to iterate it but based on condition the string which starts with m I want to print that in traditional way what we are doing here we are writing the if statement if s dot starts with m then print that s right this is out s so first let me comment all those print statement let me comment this ok so that i can show you the proper result ok this one also now i want to filter the object and i want to get the result only who starts from m ok if i run this i will get only two object marek and mac ok same thing how you represent how we can add this conditional statement with stream api right so for that there is a method called filter let me show you that filter so if you observe filter method argument is predicate so i will show you the source code of predicate also okay here you need to pass the conditional statement the same if condition you need to add here but with lambda expression right so let me show you the predicate functional interface this is what the functional interface and having a test method it will return only true or false right so let me copy this we are going to write the lambda expression for this method so that's why i am saying this predicate supplier and consumer is hugely used in this stream api so before start this stream first you should know the concept of these three right so let me show you this is what my method i don't want the prefix and method name remove it only i want the method argument okay so here i can write this is my lambda expression then i can write the conditional statement t dot starts with let me copy directly we can write the statement here and we can pass this lambda expression to the predicate okay so as this is my so we need to change it to t the same as variable name and we don't need the bracket as it is one argument okay so if i run this it will give me the two result mac and maric we are getting the two result mac and maric this filter method name itself self-explanatory like if you have to filter some data based on condition we can go for the method and filter method internally using the predicate function predicate functional interface and predicate functional having one abstract method that is the test method so you are writing the lambda expression for that test method right this is what the method present in predicate ok so let's check now how we can filter the map same way how we did for the list right so let me uncomment this ok let me comment only the first line so this is what my map it will print all the entry present in map let's check once again it is giving all the result right but i want to print only those record if the key percentile 2 equal to 0 means for even and entry set only i want to get the result so for that we need to write the filter right filter at the dot here it is accepting the predicate so for that let's write the input map contains key and value so that's why dot get key percent dial two equal equal to zero then i want to get the result only for even entry set i want to get the result right so that's why i write here if k dot get key percent dial two equal equal to zero then print that object so let me run this now it is giving only the even key right two and four so same way how you did for list same way you can iterate your map as well okay so this is what we just cover with basic example the use of forage and filter method with internal now let's understand or let's discuss one real time example where to use this filter and stream okay so so already i created a package called com.javateki.stream.api.example let me close all these three classes so here i created one employee object with id name department salary and i hardcode those value Assume this is my database layer or DAO layer we can consider. Let me add a comment. DAO layer.
Okay. And I am getting few of the employee object. And I want to write the logic to get those employee who is eligible to pay the tax and who is not eligible to pay the task. Okay. So for that I am going to write the logic in my service.
okay so let me show you that if anyone have salary more than five lakh then that guy is eligible to pay the task and those who less than five lakh he no need to pay the task okay so for that we are going to write the stream api here so let me write one method public it will return list of employee evaluate tax users you okay control shift o let's import the util First get the list from database. So as it is the static I can call with the help of class name dot get employees. It will give me the list of employee.
Okay. Then convert it to stream. Okay. Then filter it. Here we need the filter right.
We don't want all employee object. We want only the taxable user. Okay.
So here we can write EMP. whose salary more than 5 lakh okay then give me those employee so for that there is simple class collect collectors convert to list okay so if any operation you are doing and you want to perform collection to list or set you can use this predefined method okay now it will give me the let me directly return it will give me the list of employee who is eligible for tax okay now let me run this Let's make it static. So we can directly call it.
Let's print as it is going to return the list of object. Evaluate tax user. Okay. Now let me run this. We are getting 3 objects whose salary is more than 5 lakhs.
This guy having 6 lakhs and another 9 lakhs and this one having 12 lakhs. So these 3 are eligible for tax. So I want to write, I want to make it dynamic based on some input.
Either I want to return tax user and non-tax user. So let's write something input. So here what we can do if input dot equals ignore case. Let me rewrite again. Input dot equals ignore case.
tax then i want to return this okay else i want to return non-tax employee so let me copy you just need to change the condition stream which is less than five lakh okay so let me format it we can write the if else like this right so now if you run we need to pass the method argument if we will give it for tax guy it will give the same result with three object those who having more than five lakh if you will give something like non-tax then it will give the two result Okay, 5 lakh it is not considering. So we need to change the condition less than equal to 5 lakh. We are getting two result.
Okay, this guy is not eligible for tax and another one he is getting 4 lakh. So he is also not eligible for tax. okay so even we can simplify this logic as well so let's do it we can go for the ternary operator alternative to the e fails so let me remove the e fails okay we no need multiple return statement so directly we can return here this is my if condition if this condition satisfied then question mark return this okay if not then return the other one so let me format it properly ok so you can observe how we can reduce the code we used ternary operator here and we used stream operator here so if i run it will give me the result as we mentioned in non text we are getting two result if we mention tags you can get 3 result who is having more than 5 lakh this guy have 6, 9 and 12 right so normally in such case people are using the stream api whenever they are getting the data from database or some third party rest api call and they need to perform some operation to that particular object which they are getting they are going forward to use the stream api and this method right yeah So that's it about this particular video guys. If anyone have any doubt or concern you can add a comment.
And those who not subscribed yet please go ahead and subscribe it. And don't forget to press the bell icon guys so that you won't miss any update from java techie. And thanks for watching this video.