Podcast Lookup

Java Optional Explained | All Important Methods, Null Handling & Stream Integration

The Java Developers Hub

  • December 3, 2026
  • 1 h 17 min
  • Episode 24
Episode page
About this episodeHandling null values is a common challenge in Java programming. Without proper null handling, applications can encounter unexpected…
Handling null values is a common challenge in Java programming. Without proper null handling, applications can encounter unexpected NullPointerException errors that affect reliability and maintainability.Java introduced the Optional class to provide a cleaner and more expressive way of representing values that may or may not be present.In this episode, we explore Java Optional from the ground up, covering its essential methods, practical applications, and integration with the Stream API.You'll learn how to create Optional objects, safely retrieve values, provide fallback values, chain operations, and write cleaner code that reduces unnecessary null checks.Whether you're learning modern Java or improving your backend development skills, understanding Optional will help you write safer and more maintainable applications.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Optional Explained | All Important Methods, Null Handling & Stream Integration

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to understand very interesting topics about optional classes. And we will also understand how to make parallel streams in Java. Again, these two are very important topics. Both for interview perspective and when you build a real world application. Now, I just want you to keep your consistency. It's not easy to understand a series in depth. You have to put time and effort for it. And generally, the way we are continuing this series, you can see that we are going into more depth. That's why we are not just covering the code from top to bottom, we are not just reading the same things that everyone is teaching. We are actually taking the knowledge of Java, which is asked in interviews, when you build real-world applications, it is useful there. Once you get all this knowledge properly, then you will never have any problem in Java. And you will have to learn one language like this, right? So that if you ever go to learn a new language in life, then it will take you a lot of time. So, I believe that you should understand a language from the core, understand it in depth, and keep your consistency high. Being consistent always beats hard work. This motivation comes from outside. But if you remain consistent for a series, it will give you inbuilt motivation to continue the series. So, enough of knowledge. Let's go to the screen and start today's topic. So today we are going to see what are optional classes. First of all we will see this and then we will move towards parallel streams. First of all, we will talk about a very big problem of java and that is the problem of null in java. What is this problem? In java, we get a lot of null pointer exceptions. In fact, because of this, many new languages were invented like Kotlin and many more. Who said that we will solve the problem of Java's Null Pointer Exception. Now what is the problem of Null with Java? You imagine a case, let's say I told you that we have a string name. And where do we get the name? Let's say we call a function getName. We call a function getName and it gives us a name in some way Now you do some operations on this name and let's say we did any operation on it Let me tell you a simple operation, I just tried to calculate its length So I can write the length of int, I just wrote name.length here I have a method named length If I try to do something like this and by mistake this getName gives us a null name, then what will happen is that null will be stored in this name variable and as soon as I call any function or method on null, I will get null pointer exception. Now this is a big issue with Java. Java always throws us Nell Pointer Exception. Okay? So, what do we do for that? For that, we put conditions. We never write this statement directly. What do we do in Java? Before writing any such statement, we check a condition. We write something like if name is not equal to null. If the name is not null, then only we can do this work which we have done here that we can calculate the length of the name. If not, then we will not do this work. Right? Now, it seems right that whenever we have a problem in understanding that there may be a null value, we will put a null check on it. But how many null checks will you put? Let me tell you what can be a problem with us. So, imagine that we have a user class. And this user class has an address. Now address itself is a user-defined data type. And it will have a string city. Imagine something like this. So you can assume that we have multiple classes. Let's say a user class will have a class address. So the address will be a different class. Let's say A. Now I will make this address class here. In this class, there will be a string city. This is how I am asking you to imagine the hierarchy. User class, address class and city. Now assume you want to fetch the city from a user. So let's say I will give you a userU equal to getUser and this method will bring you a user somewhere in this u variable. Now what you will do is you have to city fetch from this user and you know that null can come anywhere so you will do a lot of null checks. First you will write whether the user is null or not, u equal equal null. If it is not, then you Basically, it should not be equal to null, it should be not equal to null. If it is not null, then you will enter it in the if statement. Then you will check, if I get the address from the user, then it is not null. If it is not equal to null, then you will enter it in the if statement. Then let's say you will get the user's address, after that you will have to check that the address of the user, the get address, the dot get city inside it, is it not null? Not equal to null? Right? You will check this too, then you will enter another if inside it and then you can fetch the user's city. So here you can write user.getAddress.getCity and you can store it in a string. So you can see how many checks we had to do and how ugly our code became. Multiple if statements had to be put, it had to be nested at different levels and we just had to do one thing, fetch its city from a user u. So to avoid null pointer exception in java, we had to write such very ugly code. In fact, this is what we call null check hell in java. Right? Are you applying a lot of null checks? And that You have to fetch a CT from a user and to do that you have to write such a complicated code. So here we understood what is the problem with our null. So problem with null. Now there are many more problems with null. Null means exactly nothing. You say nothing. Null means nothing. But we never knew what null means for a particular scenario. So let's say we took an example. We wrote a string. String name. And a method called getName which gives us a name If this getName method returns us null in this name variable Then we will never know what null means here Do we want to say that it is an empty name? That it doesn't exist? Is it an error? That we might have called getName through an API and there was no connection etc. So is it an error? Or is it an expected result? That maybe some user's name will be null. It won't be available in the database. So this is expected. So null never tells us a confirmation of what it is. It keeps our complete information ambiguous. That's why null pointer exceptions exist in Java. Because this name has null store, you can't call any method in it. As you do, you will get a null pointer exception. So, to solve all these problems, Java 8 brought its concept of optional classes. This is a very special class and very interesting. If you have never heard it before, you will understand it and enjoy it. How Java handled this null problem in a very simple way. So let's see what is this optional class. Now see, optional class is nothing, it is just a wrapper. It works as a wrapper for an object. So let's say this is an internal object and this is our optional class which has wrapped this internal object. What does it mean? You just understand it like this, let's say we have a string object, like in the previous example we saw string name. We made a wrapper around this, which we called optional class. Now whenever you were expecting string, we will give you optional of string. It uses generics from inside, so we will return optional of string. Something like this. Now, you must be understanding by the name optional. It means that whenever I return this, it means that there can be a value in it or not. So, may or may not be. This is not sure. May not be. So, whenever there is no sure that there can be a null value in it or not, it will return you an optional of object. Whatever the internal object is, we will wrap an optional class on it. Now, let's understand this well. What do we want to say? You just have to understand that our optional, because it uses generics, so its generic form is this, optional of t, but in this case, its form will be optional of string, right? Because we have wrapped string, what have we done here? We have wrapped an object of string, this is an object of string inside, we have wrapped it with optional above. Now, how does optional help us? It's very simple. Now, imagine what was happening earlier. The first problem was that we had a function, getName, which used to give us a name. And in what format? In string format. Now, the problem was that getName can be null sometimes, and our name can have a null store, so we have to check the null by ourselves. Now imagine that we are a caller. We are calling this method, the getName method. As soon as we call this method, it gives us a name. But we are not sure if it actually gave us a name or a null value. Because it must have been stored in this name field. So as a caller, it is our responsibility to find out if there is a null in this name field. So we write a condition on our own. If name is not equal to null, then only we have to do what we have to do. Right? We had to write a condition on our own. Basically, as a caller, it was our responsibility to check that whatever method we are calling, it should not return null. And this was very unsafe. Java said that this method is very unsafe. what will we do after Java 8? We will change the return type of our getName function. Now it will not return a string, it will return an optional of string. So let's say you can collect this in an optional of string. Optional of string. So the name of the variable will remain the same. Name equal to. So in this way you will write in your code. Because your get name will return an optional of string. And not a string. Now how is this safe? This code is safe because because the return type of getName is an optional so we understood that because it is doing an optional return it means that it may or may not contain a value and here value is a string so may or may not contain a string So basically we can't directly print the name anywhere. Or we can't directly call any function on the name. What we have to do is, this name is an optional type object. We have to get the string from inside it. What did I tell you? What is optional? There is a wrapper. So let's say this outer wrapper is our optional. And inside this we have string class. Which we have called name. So I will call it string. And this is the outer option that wrapped the string And we are giving this optional object in this name field Now if it wants a string then it will have to call an optional method which will give it this string You won't get it directly Now this is safe because java has given us some methods If you want to If you want to apply any operation or if you want to simply print the name on the screen, then what you will do is, you will ask the object of this optional that whether there is an object inside you or not. So it will tell you that I am empty or null inside or I have an object inside. If there is an object inside, then you will fetch it. If there is none, then you will skip it. You don't need to put a null check. So this means that you can write some code like this that it has a function name.ifpresent if present and it expects a lambda from you and what type of lambda does it expect? consumer type so you can call it directly if the name is present then do one thing system.out and here I am using method reference println I will write it as println. It means that if you have any value inside, then print it directly. Basically, its string value. So, as soon as I write this value, as soon as I write this syntax, what will happen? My optional class, as soon as I saw if-present, my optional class will see if I really have a value inside. This answer will say yes or no. Basically, true or false. If its answer is true, then this statement will be executed. And if it is false, then this statement will not be executed. So now we have the checks that we have to write if name is not equal to null then do this else do something else etc etc. We will just check if the name is present. If it is then it is good. Print it. If it is not then it is okay. Leave it. So this benefit was given to us by optional class. We will wrap any object and return it. Wrapper means that when you see optional, you understand that there may be value in it or not. Basically, the one you are calling is not sure. GetName is not sure that I will give you the right value or not. That's why I have given you optional. Optional means, you might get the value, the exact name, or you might get NULL. Now, your return types have changed. So, whenever getName returns a string, I'll be 100% sure that getName wants to say that I can never get NULL. So, this is a good practice whenever you write code and you know that the return I'm doing with this method, I can sometimes return NULL as well. So, don't actually return any object, let's say string or integer, don't return anything. Return optional of string or optional of integer. Basically, wrap your own object inside an optional object. Now, this can be anything. Let's say, this can be your previous data type, string, integer, etc. Or it can be your own user-defined data type. Let's say, student. This is your own data type, student. You have wrapped it with an optional object. So, let's see what is good design practice using optional. So, what was the first thing? Let's say, if we want to store null in a string type, then we directly write string name equal to null. But now this is a wrong approach. We should not directly store null in a string. If we want to put null, then we should wrap this name in an optional class. For that, we have a very good syntax. So, first I'll write that this is our bad design. And the interesting syntax we have is that we make an optional of string. So, we wrapped a string class in an optional. Let's name it name. And here we just do optional.empty Optional.empty. Just write this. By writing this line, what will happen is that we will say that we are making an optional object. We are making an optional object. The string inside it is null. That means it is empty. And we can return this to anyone. Now what you have to do is, whenever you return this name, and whoever gets it, whoever is the caller, they will know that you have returned an optional name. This means that it may be null. So they will just have to check that name.ifpresent. So with this condition, they will check whether the data is present or not. Here they can expect a consumer. But if the load is not expected, then your code is simplified. You just have to keep a good practice that whenever you have kept an optional return type, that means you are telling the caller that I can be null. And if you have kept an optional return type, then you are telling the caller that I can never be null, I will always give you a value. So this works as a contract. Now this was the method if you want to actually store nulls. But if you want to actually store values, what will you write? You will write something like this. So let's say you will write optional of string, let's say string name equal to. If you want to add value to this, then you have a function optional dot of optional dot of and here let's say you can put a value I have put aditya here so what I did here is I called optional dot of a method and passed aditya in it so what I am saying here is that I have a string whose value is aditya this is a string now string is obviously an object I have wrapped this object in my optional class and now if I have to send then I will do this whole optional class so actually there is value in it but I will still send optional class tomorrow I will have to check someone he will directly check that name.ifpresent and the answer will be true. If present is there, then whatever we have written in if present will be executed. Now this optional class, once we think conceptually, how will it look? It's very simple, you can imagine. We will have in Java a class optional, optional, which obviously uses generics, so it will take t. And what is t? It is trying to wrap it. Right? And let's say it will have only one variable, private final t type of whose name is value. Something like this. So, I will make it smaller. This is our complete optional class. And here, Java will always tell us, in two cases, that the value written in one case is equal to null. If this is the case, it means that we don't have the value present inside. We will say that our optional class is empty. But if the value in the other case is not equal to null, In that, we will say that the value of our optional class is present. Okay? Something like this. It just tells us two things, whether the value is empty or present. It just gives you two answers, true or false. And it will depend on whether the value field is null or not. That's it. What did you do? You wrapped a simple object inside an optional object. Very simple. With such a simple design, it reduced the null pointer exception. How? If you follow good practice, As I said, you should always return optional, wherever you are not sure that the function you are returning can be null. So, the caller will always have a guarantee that if I see the option written, it means that whatever I am getting, whatever string or any object I am getting, there are chances of being null. So I can't directly access it. I will write dot if present. This way I will access it. So I won't have to do a lot of null checks. And I will always be sure. I will be safe. Because if I forgot null check. And I directly did name dot length. So I got null pointer exception. But in this case I can't do name dot length. Why can't I? Because here the name is not a string. It is an optional string. And I can't call length on that optional. Because it doesn't have any function of length. I can't call length on that optional. First, I can fetch the name. I can fetch the value inside the option of the name. And I can call the length on that. And to fetch the value, I have to call my method, dot if present. So, Java has made it mandatory that you can never put an operator on this name. If you want to call a method, then first fetch the string inside it. Okay? Now let's see some methods of optional. It is very simple and very limited. So you won't have any problem in understanding. First of all, let's see how we can make optional. So creatingOptional. See, one way is simple which I have told you. You will directly write optional.of and here you can pass anything. Let's say I passed a string. Aditya. So what did I make here? Here I made an optional of string. Something like this. Right. call it anything, let's say n So I made an optional of string. Now this optional.off is very strict because if you pass null instead of aditya then you will get an exception. If you are writing off then you can't write null inside. You will have to give an object. If this is an optional string then you will have to give a string inside. But if you want to declare optional in such a way that it can take value as well as null. Because it is possible that you are not sure whether you will get value or null. It is possible that you expect this value from a third function. So in that case, you can change your syntax a little bit. You will write, let's say, so optional string n equal to, you will write optional dot of nullable. This is a new method of nullable and it also expects some value. Let's say, I can write aditya here as well. No problem. But the interesting thing is that I can write null instead of aditya. Both cases will work. No problem. So, it can take either value or null. So, use optional.offNullable when you are not sure whether you want to give value or null. It may depend on any condition. So, you can use this. Again, if I want value at this end, I will have to check ifPresent. present and here I can pass a lambda function of consumer type and if my data is present then it will work but if null is present then it won't work but I can't do anything with this n basically I can't call length on this n so n.length Because this length function is the function of my string But n is of optional type So I can't call this on it Now I can't directly print this n Right I can print it again The name of the class will be printed But I want my string to be printed But I can't do that directly What I have to do is Go inside this optional And fetch this string Okay For that I have told you The method is ifpresent And the third method is optional.empty. If you are 110% sure that you want to return null, then you can use optional.empty. Generally, you want to say that the object wrapped in optional is actually null. Again, whatever variable you assign to it, let's say n, you can check it by doing n.ifpresent. If the value is present here, then whatever lambda you pass here will be executed. If not, then lambda will not be executed. Now, this was how you can create optionals. But let's say someone created it, the caller created it. Now, you can call all the methods in it. So, basically, getting value from optional. So let's see which methods we can call. And the simplest and the most dangerous method is GET. You simply call GET on it. So let's say you receive optional of string. Name And what did you call? Let's say You called a function GetName Right? This getName method Is safe And it follows good practice So it told me That my return type Is optional of string So I'm saying That I can be null So check it Take precautions For me before executing it. So what could you call? You couldn't directly access this name. You have to see if there is a real value in it or not. For which I told you, the name of the method is name.ifpresent. But it's not necessary, you always call ifpresent. There are other methods in Java which you can call in this optional class. One of them is get. So what if you directly call name.get. Now this is unsafe because name.get will give you a string name Let's say I call it name do because again this is also a name. Right? So name.get means exactly this. That your optional class, whatever object is in it, it will get you. It will bring you and give you. Now this optional, what kind of object is in it? String type. We have declared string here. So it will get you and give you. But only then it will be able to get you. If the object in optional actually has value. But if it has null in it, then it would give you an exception. And the name of that exception would be No such element exception. So again, we have to use this with a lot of precaution. We don't generally prefer to use name.get directly. Because what is the benefit of this? Again, we have brought so many options because we don't want to get any such exception. Now if we don't get that, we get another exception. No such element exception. But we are doing the same thing. In that case also, we used to receive string name equal to get name. So, we used to receive string from get name. And it used to return null. And if we perform any operation in this name, let's say we performed Lens, and its name will be null, then we will get null pointer exception. So, we are trying to fetch the name directly. Now apart from GET, there is another good method which is ISPresent. This is different from IFPresent which we just saw. This is ISPresent. And you can use GET and IFPresent together to be safe. So you can write some code like this. Let's say, again I assume that you have an optional of string which you receive. Optional of string, let's say name equal to get name So get name gives you an optional of string And to verify that name actually has value, you can write it like this If name.isPresent, means in this name, in this optional, actually value is present And then you can do name.get, name.get Right, in which? In any string, let's say string You got it in S. So this is a safer alternative. Here you won't get no such element exception. But again, what is the benefit of this? It would be better if I wrote it like this. If name is not equal to null. Right? Then you can work with the name So this is also the same condition, I have to put an if here as well If I check null only, then this is the only alternative The advantage here is that if you don't put the check of name is not equal to null And directly try to fetch the name Then you get a null pointer exception But you can't do that here You can't do anything directly on this name Because it is not a name string, it is optional If you want to work, then you have to get the name.get or somehow get the value. But if you get the name.get and it gets null, then again you will get no such element exception. So what I mean to say is that we have slightly improved the code, but we still have to work that much longer. Right? To avoid all this, Java introduced its third method, which we had seen till now, which is ifPresent. IfPresent. Okay? This was the upper one, isPresent and this is ifPresent. Right? Because it takes a lambda in the statement as we saw. So again, if you have an optional of string, let's say getName, then you can check something like this, name.ifPresent and here you can give some lambda. Let's say you have to calculate the length of the name or let's say you have to print the name on the screen, then you can simply write system.out. And here you have written method reference operator and println. Print it. Simple. Its basic meaning is simple. If present means if value exists, then execute this code. If value doesn't exist, then there is no need to execute this code. So, we have seen three methods. We saw get, isPresent, and ifPresent. Are there any other methods that exist? The answer is yes. Interestingly, there are new methods that are used a lot these days. I will tell you the names of those methods. You can call another method in this optional class whose name is or else. or else, this is a very interesting method or else also fetch your value in this way, let's say you have a name of optional type and you write dot or else and let's say you pass a default string here, let's say I take the name unknown unknown something like this and you collect it in a string, let's say s you know what will be happening above, we will collect it like this, we will have optional of string Let's say name, and how do we collect this name? With the get name method. Get name, right? Now you know what it does, it returns an optional string. If you do name.or else on this name, and pass a demo string here, let's say we passed unknown, it means that if there is actually a value in this name, then that's fine, that value will come in this s, but if not, then unknown will come in it, the default value will come, right? So this is your optional class, it has your object, right? Your string object. If there is an actual value in it, let's say Aditya So Aditya should come in this s But if there is a null in it, then this variable unknown should come in this s Which we have passed as the default parameter So this is again very interesting Now as we have or else, we have one more method or else get Now I will tell you what it does and how it is different from this Apart from this, we have one more method or else throw. Let me tell you what it does. Apart from this, we have another method. If present or else. You must be able to understand this from the name itself. What did it do? It combined or else and if present together. Now see, or else get works just like or else. Basically, I can write name dot or else get. And here I'll pass a demo string. Default string, let's say unknown. ok it has the same syntax now see or else get works just like or else it just changes the syntax a little ok and what is this different we will see that again in the code again what does or else throw do it also works just like or else but here you don't give a default value and throw it so or else throw means If in this name field, if I have put name dot or else throw, then if the value exists in the name, then it is fine. If it doesn't, then we will throw an exception directly. Again, which exception? No such element exception. Okay. So this is exactly the same. If you want, you can throw an exception. You will use this. Again, we will see this in the code. And finally, if present or else. You must be understanding this. If it is present, then again, you will give a consumer here. That method will be called or object. Vola method or Vola code will be called. And if not, then whatever default value we have given will go. So basically, whatever methods we have talked about, whether it is get, isPresent, ifPresent, or else, or else get, or else row, or isPresent, or else. Let's use all of these in code. Look, first of all, let me make a method. Let's say public static optional of string optional of string whose name I will keep as getName because this is the one who was giving us the name Now again I will have to import the optional first So I will import my optional class And what I will do in this function Let's say I will return for now Optional.of Aditya We know we can assign value like this So I returned it Now I was saying that I want to receive this value here So I can again receive in an optional of string Let's say name equal to GetName, I was calling it like this in my code as well. Now I am saying that if I want to fetch value from it, then I have a simple method. I can simply write it, let's say I want to print it. So I will write name.get, simply. So if I run it, I should get it. And I got it. But the problem is that if I send optional.empty instead of optional.off And I don't send any string here. So in that case, my code will be broken. Because what will name.get do? It will throw an exception. So let's run it and see. So now I have no such element exception, right? About which we had discussed. Okay. So what should I do? One safe alternative is to check it first. If, let's say, name.isPresent. If it is present, then what should I do with system.out.println? name.get. Now my problem is safe. So if it is present, then it will be printed. If not, then I will skip it. So in this case, nothing should be printed. So let's run it and see. Nothing happened. But again, if it was optional.off and here the value would be present, Aditya. Now if I run it, So my Aditya would have been printed. Right? So it's very simple. But our code is still looking verbose. So I'll comment it. There's a better way to check it, which I told you. I can write name.ifpresent. Now what I said, it takes a consumer type object. So if I hover over it, then ifpresent takes a consumer object. Okay, so I'll give it a consumer. Let's say system.out println. So here I have passed method reference. So now let's see if my name will be printed. It should be because it is present in it. Let's say I null it. So instead of that I can send empty. Or I have another function of nullable. Because the advantage of this is that I can also send null here directly. Something like this. This code will work for me. But again optional.offNullable means I am sending null here. So null will come in name. Basically empty will come. Now when I will present if in that empty. So again false answer will come. Means this will not execute anything. So in this case nothing should appear on my screen. But again no exception should appear. Now again we have one more method. So I will comment it again. I have one more method named dot or else. And here I can give a default value. Let's say unknown. So I said if someone doesn't have a name then unknown should be printed. And I will write this in system.out.println. We have to remove this. Now, because nullable is coming, my screen should be printed as unknown. Right? Because what will the or else method do? Again, it will look inside the optional. All the methods inside the optional, in which we are trying to fetch the value, what will they do? They will look inside the object of the optional class, that the object inside which I am wrapping, will it have the real value or is it null? In this case, when it will look inside the or else, it will understand that it has null inside it. So, it will print unknown. Okay? So let's run this and we get the value of unknown And if the value was aditya then we would get the value of aditya instead of unknown Something like this Now let's comment this and we have another interesting method which we called name.OrElseGet If you go inside it, or else get, then it asks you for a supplier type object. Supplier, you know, is a functional interface. So let's say, I want that if nothing happens, basically if we look in the name, if we look in our wrapper class and we get null, then what will we do? We will give an unknown value here. So I can do this in supplier that I don't give anything and return a string whose name is let's say unknown. Unknown. Something like this. Now again because it takes supplier, supplier means that you take a void in the parameter and return an object. So I made it empty here, maps to unknown. If I had just written else, I would have simply passed a string here. Let's print this once, I haven't printed it yet. So let's do one thing, let's write this inside system.out.println, something like this. Now because get name is giving me Aditya, so Aditya should be printed on my screen. Let's see once. Aditya is absolutely correct. But suppose if this was null. What would happen in that case? In that case, I think my unknown should be printed. Absolutely correct. Right? So if this and this are the same, let's say something like this. Now here I am just using or else, which takes a string directly. And here I am using or else get, which takes a supplier. What is the difference between these two? Now the difference is, this or else will always evaluate the other, which is taking a string. Whether there is value in it or not. Evaluate means it will actually look at it. It will see what value is there in it. But the or else get method. If there is some value in optional.of nullable. Let's say here instead of null there will be aditya. In that case it will not evaluate the supplier. So you can understand it as short circuiting. That if there is value then it will not evaluate it. But or else whether there is value or not. Whether there is value or not it will evaluate it. So the advantage of this is that if you know that the operation is very expensive, in that case you can write or else get or else you can simply run the work with or else. Now you know that apart from this you had one more which was or else throw. Sorry, you will write like this, you will put it on the name. So name dot or else or else throw. Something like this. So you say that if there is value in the name, then it is fine, print it. Otherwise, throw the exception. Now there is value here. So, Aditya should be printed. But, if the value is null, then we would have an exception. So, let's run it and see. No such element exception. We have thrown it. So, in this case, we have deliberately thrown an exception because we want to throw an exception. Right? Now, again, finally, we have another method. Which is name dot if present or else. So again, what does it take? What is its parameter? One, it takes a consumer, which also takes if present. And again, it takes an empty action of a runnable type. Okay, I'll tell you what that is. Now see, what does if present or else do? The consumer can also take from you that the code that has to be applied, if the data is present, then I simply write here system.out println, which I had written earlier. Something like this. And if my present is not there, then what will happen in else? I can tell you that too. Normal, if my present was there, if there was value in this getName, then this code would have been executed. I mean, my value would have been printed on the screen. And if it wasn't there, then we would have skipped it. But in this case, we are not skipping anything. I will comment it. I am not skipping anything in this case. I am simply writing name.ifpresent or else. If it is there, then do this. If it is not there, then do something like this. So in this, I will give again a lambda express so basically system.out.println let's say unknown unknown here it became unknown so I will make it unknown So I will do unknown something like this So I wrote here if present or else If there is a value then print that value If not then print unknown In our case print unknown Because we are giving null But if I will give any value here Let's say I gave here Let's say I gave here aditya Then our print should be aditya Absolutely right Again we have seen all the methods Which we can apply in our optional class If you understood all this So, let's see one more interesting thing which is Optional Transformation. It says that we can apply some transformations on Optional too. Like we used to do in our stream class. Okay, which transformation? Let's see. We generally apply three methods. One is Map. The other is flat map. Right. And the other is filter. In fact, we have seen these three methods in streams. Here also, they work almost the same way. But we can apply all of these in our optional. And this map, flat map and filter, because they come in the optional and work like intermediate functions, so what do they return? They return an optional type of object. Right? So that I can further chain on them. Like in our stream, it was the same principle. First, let's understand why we need these operations in our optional. Let's assume that we have a user class. In this user class, we have an address class object and in this address class, we have a string city. And I have to access this city And I don't know which one of these will get null So I had to apply a lot of conditions I used to check if user is not equal to null After that I will come to this loop Then I will fetch if let's say user dot getAddress is not equal to null, right? Something like this. Then I'll come inside this and then I'll check that user.getAddress.getCity is not equal to null and then I can use it in that if, etc. So I had to put multiple ifs. So I introduced optional only so that I don't have to do all this. I can simply check if there is any value inside any object, inside any optional wrapper. Maybe or maybe not. Okay. So look, these three methods, map, flatMap and filter, they help a lot to simplify this complex task a lot. How again? Again, if I put my chaining of operations. If I show you a reflection of this, how will this code look? If I put chaining in this user class example, it will look like this. Let's say I get a user from somewhere, so I will write optional of user. Let's say its name is small user equal to get user. So get user name will be a method which will bring me a user and not a user but an optional of user. That maybe there is a user in it or maybe there is no user in it. Now if I want to go inside this user, go inside the address and fetch city. So I can simply write, let's say first of all I will write user.map. I have applied map on this user. Now you know what map does. It takes a function type interface. So I can write lambda on it. So for let's say x such that x.getAddress Something like this. Do you know what x is? In our case, x is a user, right? So I can map it. For every user, whatever we are getting, bring its address. Here I can again write. map. Now here I would have got its address. So I can write for every y, which is an address, y.getCity. Bring me city. I would have got its city here. After that, if my city is here, then I can simply check if in the name of present, If present, let's say Z. I just need to do system.out.println. That means print this Z on the screen. And what will Z be? It will be a string. Because what did I get from get address? I got an address, so Y is an address. Now what did I get from y.getcity? I got a city, Z, which is a city. Now if I get a city on the screen, if it is present, then I can print it. In fact, I can replace this whole thing using method reference. So I can write something like this, user.map. and here I will write user method reference operator get address right then next time I will write dot map now I have got the address so I can write address method reference operator let's say city let's say get city right and next time I can write if present if city is present then simply system dot out you know this here method reference operator println println something like this so basically I can do all these things, you see how much my code has become more readable than this how much my code has become more readable than this whether I put lambda or method interface whether I put method reference but in comparison to this I write so many checks here my code is very readable right so all this helps my optional transformations in which map, flat map and filter are three important methods Let's quickly go through the code and see how it works You will get to know a lot In fact, you will understand a very good thing That this optional is also a kind of mini stream We will talk about this, first let's see all these methods because it is very interesting So first of all, I have made a demo2. java file I will go to the use case which I was saying repeatedly Let's say I have a user class Now suppose in this user class I have an address type object So public Address Let's say address in small Now what is this address? I will make a different class for it Let's say address Let's say address something like this And there is a string in this address Let's say public string City, something like this Now I can private all of them and give getter and setter But for now I have made all of them public so that I can access them directly So there is address in user and city in address Now assume that I have a method Let's say private User GetUser Which returns you a user So here I can return you a user So I can write return new user return new user. Something like this. Now because I am returning an empty user, I can fill in some values and return them. Or I can make a constructor on it. But again I don't want to make a constructor. I will write it directly here. So I can work. So let's say user u equal to new user. I made a user by doing this. Before that let's say I make an address. Address a equal to new address. a equal to new new address. Something like this. And the city in this address, a.city, let's call it Delhi because it can store a string. And I can do one more thing. In this user, u.address is address equal to a. Something like this. So first of all, I made the address a and put Delhi in the city. Then I made a new user and put this address a in its address. And finally I can do return u. I returned this user. Ok, so nothing happened. I just filled in the values. I could have filled it with the constructor if I wanted. But I filled it directly here. Ok. Now let's go to our code and collect this user data. So look, if I understand it in my traditional way, then I will write user user equal to get user. Get user, and again I should make it static. Okay, public static. Yes, error is gone. Okay. Now see, what I have to do, I just have to print city. And I have a risk that wherever I go, anything can get null. So I will take a lot of precautions. So I will take all the precautions. So let's say first of all I will check if user is not equal to null. User should not be null. If it is not null, then I will come inside. And after that what I will do, whatever address it has, I will take my address in this field, in address. So address address equal to user dot address. So I got the address. Now I have to check if my address is null. If address is not equal to null, then I will go inside. If the address is not null, then I will get the string city. And how will I get the string city? From this address.city. Once I get the city, then I have to check if the city is null. If city is not equal to null, then I can print my city on the screen. I have written such a long code here, now if I print it, then my Delhi city should be printed. It is definitely printed. But look at my code, how much it is verbose and ugly. I will do this code comment. I don't want to write such a code. I want to write a better code. The responsibility of this getUser method is that it tells me that sometimes I can give null. So in that case, it is not a user but an optional of user return kare optional of user kuch iss tarike se yaha pe mai kar leta hu isko pehle import theke toh mujhe ye return kare toh again mai isko yaha pe directly user ke andar ne le sakta mai le sakta hu optional of user ke andar user equal to get user theke ab dekho maine kya kaha mera code kitna simplify ho jayega mujhe bas likhna padega let's say user dot map aur yaha pe mai pass kar dunga x such that x dot Address will be something like this Then again I will apply dot map again And here I will write y such that y.city because I will give the address And then I will apply let's say dot if present Because if it is not present then I will not print If present then z such that And let's just pass method reference here System.out Println println something like this and I'll close it here's an error, I've written two dots here I've returned u here but I can't return u I'll have to return optional dot of u something like this, now I don't have any error now if I run it and see So I should get Delhi, which is expected. But see how much my code has been simplified compared to this. Here my code was looking so complicated, I didn't understand what I was trying to do. In fact, I have now simplified my code completely.. map,. map,. ifpresent. Now let's go and see all its methods properly. So first of all, our function is. map. And we know what it does in streams. So it takes a function type interface, which is a function interface, right? Instead of that, you can pass a lambda expression to it. And you know the function, that's why it's also called a transformation function. Basically, it can convert a T to a T to a R. We have seen this, right? So here we are doing the same thing. We wrote user.map here and see what we gave. Convert an x to x.address, right? As soon as I write user.map, what will happen from inside? This optional of user, where we will see in this optional class, what is there? User is there. So here x is a user because there can be a user in optional. So I am mapping x, basically I am mapping user that bring me the address in it. So I got its address. Then again I am mapping it. See here I have passed another function. That whatever address I have, bring me its city. So I got the city. Now city is a string. If it is a string, then I can directly check if it is present. If it is present, then directly print it. If it is not present, then you skip it. Right. So now think. Doesn't this map work in the same way as it does in streams? The only difference is that our streams have multiple values. So if I explain it like this, our streams in Java have multiple values. Let's say it is working on a collection, it has a lot of values, 1, 2, 3, 4, 5, 6, 7, 8, 9. And it is working on these streams. So let's say 1, 2, 3, 4, etc. But, my optional class has the same value. I have the same value in my optional class which is user. If I represent it like this, this is the wrapper of my optional class. It has the same value which is user. So, when I do dot map here, I am mapping on this same user. Here, I am mapping on a lot of numbers. If I talk about users instead of numbers, I will talk about a lot of users. User 1, user 2, user 3, something like this. And dot dot dot. There can be many users in this. But the option will work for only one user. So suppose I have only one of these. I will work on that. So whatever operation I perform, whether it is a map, it will be on that. Whether it is a filter that I will show you later, it will also apply on that. So I can write on this that my map function is in the option. So if value is present, if the value is present in it, then apply the map. Whatever the map is saying, it will apply. Apply the map and return Optional So again, this user.map will return another Optional so that I can put a map on it Then it will again return Optional so that I can put a method on it like ifpresent So if I go inside it, there is a map method inside my optional.java class Which again expects a function and returns an Optional Same here too, both maps are applied, so both options are returned. Now this if-present, you can assume that this is like a terminal expression of streams. So to say, this optional works as a mini-stream, which has only one value. Streams have multiple, it has only one value. So if the same value is present, then we will apply the map in it, and we will return optional. Else, if there is no value present in it, it will remain empty. Right? Something like this. Now you see, here we have applied. map. I have another method in streams which is not there. I have another method in my optional which is called flat map. Now remember what flat map used to do in streams. It used to flat any stream. So if my data is two dimensional or multi-dimensional, that there is a list in my list, then by using flat map, that entire list used to flatten. Same thing will happen. Now assume that my user doesn't have a simple address. It is an optional address. Address. A double D R E double S. Optional of address. Something like this. Okay. So whenever you try to print address. You don't get address directly. You get optional of address. Okay. So I have to change something here. I can do optional dot of A in U dot address. Something like this. Okay. So my error went away. Now I can't do it like this, obviously I can't put a map. Now there will be an error here as well. First of all, you understand what we have. We have a user class, which has an optional of address and a city inside the address. So this means, when I was writing the syntax like this, for every x maps to x.address, I can't write this. Because this line is not giving me the address, but it is giving me an optional of optional address. So when I write x.address, it is not giving me an optional of address from inside. Because you know that user.map gives you an optional. Whose optional? Is the address optional? The answer is no. It will give me an optional of optional of address. Let me tell you what is the mistake here. Now see, I am not able to apply a queue here. Let me tell you. You imagine this. Let's comment on this for now. You imagine this. When I write user.map something like this. And here I write for every x map it to x.address Something like this If I write user.map 4x map it to x.address, then what will be its return type? Now look, I am saying bring me the address of that user x. But do you know what is the return type of this address? The return type of this address is optional of address. So this line will bring me the optional of address. And what does map do? Map brings an optional of whatever you return. So basically you can't say that it is bringing me optional of address. Something like this. Right? Something like this would be happening. Right? If my option was not here, like the normal address would have been written here, then this return type would have been its return type, just optional of address, right? Because this line would have given address and the map would have wrapped it in an optional. So if I write it like this, optional of, optional of, optional of address. then my statement will be absolutely correct. Let me give it a name. A-double-D-R-E-double-S address. Then my statement will be absolutely correct. Because it is doing the same thing. It is giving me optional of optional of address. But if I just write optional of address, then it will be wrong. A-double-D-R-E-double-S. It will be wrong. It will simply tell me type mismatch. Type mismatch. Okay. So I cannot do this. I will have to take it optional of optional of address. So if I have to do this, then you must have understood that I can't simply do this. Because if it is giving me this option of option of address, I can't directly apply another map to it. I am saying for every y map it to city, but I can't map it to city, I am getting this. I am not getting simple option of address that I can go inside and fetch city. In that case, my flat map is useful. What I will do is, I will put flat map instead of this map. So you can see that my error is gone. If I comment on it, you can see that my error is gone. Now what would this flatMap have done? I will show you by putting it here. If I had put flatMap instead of user.map So it returns the same thing that I was expecting. Optional of address. Right? So it returns the same thing. You can see that I didn't get any error. So flatMap again does the same thing that it does in streams. What does it do in streams? It flattens our multiple dimensional data. It also flattens our multiple optional data. Right? So how was it? I have a user and in that user there was not a direct address but an optional address. So you can understand that the user is an object and inside that there is an optional object which has an address inside it. Because optional wraps the address and inside the user there is an optional object so I can assume that the user is wrapping it. So there is an option in the user and an address in it. So there are multiple dimensions here. So this flat map flattened the dimensions of the optional. So for this, it assumed that the address is at the level of the user. So it assumed that the address is at the level of the user and not the optional. So the address is at the level of the optional. Like in streams, it takes two-dimensional to one-dimensional level. So, by doing flat map, our error is gone. So, you understand, whenever there is another object as an optional in one object, you should always use flat map. Otherwise, again, you will have that problem that you will get optional of optional of any type. Like here, address. Okay. Before this, you can simply use map. Because once you flat it, then you get straight address. It is called optional of address. And in that, I can fetch y, which is city. And again, I can print things in it. Okay. Now again we have a third method which is called filter. And again this is very simple, it takes a predicate like it used to work in streams, it will work in the same way here. Now it will do the same thing, it will check a condition. And if true, then it will keep the value else it will not do anything, it will not let us go ahead. So basically if we apply filter in streams, then it would have multiple values, so those who would pass through that filter, those who could not pass, we would discard them there. But in optional, because there is only one user, So let's assume that it is trying to pass through this filter. If it passes through this filter, then we will apply another operation on it. And if it fails to pass through the filter, then we will discard it. That's what I have written, check a condition, if true, keep the value. So if it passes through that condition, then basically the user will remain as it is inside the optional wrapper, it will not come out. But if it fails to pass, then it will come out of the optional wrapper and our optional will be empty. So in that case, we will not perform any operation. So, I will give you an example of this. Let's say I am not using all these. I will remove all these. I will comment. Okay. So, I am not using any object of my user class. I will comment this too. So, my main function is completely empty. I am writing some new code here. So, you have to pay attention to this. What if I write that I have an optional of string. Optional of string let's say name equal to and I take value from optional dot optional dot off and pass a string named aditya something like this If I perform some operation on it, then let's say again optional of string, I get a result and what should I do? Apply name.filter on this name. Again, you can take a predicate here, so I can write for every x, map it to. Again, you have to think like this, what is this x? You consider this optional as a stream. You imagine if I had written stream here, then it would have a lot of value. But because it is written optional here, then it has only one value and that value is of string. Basically, there is only one string in it. You can assume that if you have a stream in which there is only one string, then it is Hello or Aditya. So, it is the same. Here you have an optional string in which there is only one string, Aditya. So, I am saying that for every x, whatever x is there in this optional, there is only one x in this optional, whose name is Aditya. So, basically, I am looping it. For every x, basically, on this Aditya. So, map it to, if you give me a predicate, then I will give a condition. x.length, let's say, greater than Let's say 10, something like this. So what I am saying is that I am filtering out. If the length of the string is more than 10, I will let the string go ahead. Otherwise, I won't let it go. I have only one string, Aditya. Is its length more than 10? Answer is no. So I won't let it go ahead of this filter. That means nothing will come on my result. Empty will come on my result. So I know name.filter, any operation like this filter, which is made in optional, it will return an optional type object. and wrap it in the option. So this will also return an option. But because the filter didn't let it go ahead, so this will return an empty option to our result. So if this is empty, then if I print here result, let's say result dot or else, which if empty, then it will print what I am writing here and simply write empty. So my screen should be printed empty. So let's run it and see. And I got empty. But if I change the condition and write greater than 5. So here I have 6 characters, right? So this condition will be true. I should get Aditya in my result. So if I print it. Then I will get Aditya. So you don't have to understand optional differently. Now imagine that this is a stream. I have written here stream. Now imagine like this. Now what happens in stream? I have multiple values. If I had stream of string, then it would have a lot of strings. Aditya could have been Rohit, Rohan etc. There could have been a lot of strings. And I filter on all of them. So when I write. filter, the strings pass through that filter one by one. Which we saw in our streams, there is vertical loading. First Aditya passes through the stream, then Rohit, then Rohan etc etc. But here we have an optional of type string. This means I will have only one value Aditya. So it works like a mini stream, in which there is only one value Aditya. And whatever operation I will put, I will put it on the same string. So, as I wrote name.filter, I am talking about this string. For every x, there is only one x here which is aditya. So, for this aditya, if its length is more than 5, then it is fine. I will wrap it in optional. And if it is not, then I will wrap it in empty optional and assign the result. Right? I can check on the result whether there is any value or not. So, I have a method called or else. You know this. If I give default value here, then default will be printed. If the result is empty, and if it is not empty, then whatever is filled in it, that value will be printed. In fact, I can also combine my filter and map. So if I show you an example of that, then you tell me what should be printed here. So let's say I pick this up first. I need this. An optional of string. Let's say I write name.map on it. So x maps to x.length. I have to find its length. Then I write. filter. Now here comes the length, so I have a number. So what can I do with that length? Let's say for every length, length should be greater than, let's say 10. And finally I write dot isPresent. What is present? Because again this will also give me an option. So again what I was doing here, I am doing it in three lines. Dot ifPresent. IsPresent, so I will simply write system.out to our method reference operator println. Something like this. So basically, what I was doing here in two times was that first I would filter the name dot, then I would give it in the result and then check the result dot or else. I could also check this present here. So I did it like this. Here, if present will come. So I did it like this. So you tell me, pause it once and think what answer should come here. It's very simple, right? What I did is, I applied a map in name. This is my name. It has only one value in it. Obviously, there is only one value in optional. I calculated its length. Now its length is 6. So, as soon as I called this method, it returned me 6. Right? It returned a number, an integer. Now I put a filter on it. Whatever number I get, I have to check whether it is 10 or more. The answer is false. It is not 10 or more. Now as soon as it comes, it is false. How do you understand stream? You say in stream, I will not move ahead. But what happened in the case of optional? As soon as it was false, my filter was false, So it returned an empty option. Now if I check ifpresent on that empty option, then ifpresent will be false. That means this lambda expression will not be printed. What is this lambda expression of consumer type? This will not be printed on the screen. So the answer is that nothing will be printed on the screen. If I will run it, then I will not get anything. But if I would have reduced this condition to 4, greater than 4 then it should have been printed on my screen, its length, because last time I was checking the length. Now see, x.length gave me a number, length, right? I wrapped it in optional, then the filter wrapped it in optional by checking this condition again. Now this condition is passed here because my length is 6, Aditya's, and it is greater than 4. So again this condition, So again this condition became true So it wrapped that 6 in optional And gave it to ifpresent And ifpresent saw that does it have any value The answer is yes, it has 6 values in it So if I print this Then I will get value of 6 Right? Absolutely correct If you understood this Then here we have to draw a very simple conclusion That our optional Is like a mini stream Mini stream Okay? If you put this in your mind, everything will be clear. The normal stream we have has multiple values, but the optional has only one value. So it plays with this. In fact, I can say that either the optional will have one value or no value. The case of no value which we call null or our optional will be empty. Or it will have one value which it will wrap. So imagine that this is my optional object. And it will wrap another object. So this is my internal object which is wrapped. So now I am saying that this internal object will either contain value or will contain null. So if it contains null, I will say that my option is empty. And if it contains value, I will say that value is present in my option. Something like this. So these are the two scenarios that we can play on. So all the operations that we apply here, whether we apply our map operation, filter operation, or flat map operation, we apply these three on the same value which is present inside this option. I hope you have understood well what is an optional class, what methods we can call in it, how we can access it. It is very simple, it has very limited methods. If you have understood it well, then it is a very powerful tool. Powerful because when you combine optional with streams, So it's fun. Now we will try it. First of all, I want to give you some guidelines of optional. And that is that some people think that optional is so good that we can use it everywhere. Now we will not use null anywhere, we will use optional everywhere. But this is absolutely wrong. Because when optional was made, it was made with the principle that any such method If an object returns something, let's say we have a method m1, right? And this returns something, an object, right? And we don't know that sometimes that object can be null and sometimes it has a value present in it. Then we had said that for this case, what we will do is, we will ask it to return an optional object. Optional of that object t, right? T can be anything, return that. Right? So this was basically made as a return type. Now some people use it as data types. Data types. So basically what they do is, let's say there is a class, we have made a class, class x, okay, in that they will do a data type, let's say name, and that name will be optional of string, optional of, let's say optional of string name, they will do like this. And although you can do this, it is allowed, there will be no error, but this is not a good practice. This optional of string should not work as a data type, because its work was just to return. This should not be a data type in itself. If you wrap everything with optional, then think what will happen. First of all, string is an object itself, so you have to object and wrapped it optionally. Now, if there are other objects in X, you will make that too and wrap it optionally. Then you will make something and wrap it optionally. So again, you will have to fill optional in the entire program. That means, everywhere you have to call ifpresent, or else, or any other function to get the value. So there is no use of all this. It is always better to use the existing data type string. Use optional only if you know that null can also be returned in any method and you don't want that. So you can return optional of that t. Like you can return optional of string. Okay? Now, if we compare it with its streams and try to combine it with streams and optional, then think about it carefully. Our stream deals 0 to many elements. And our optional deals 0 and 1 element. There is nothing else. Either 0 element or 1 element. If we combine both of them Then we can't say that we are handling Maybe value Inside multiple values What is your option? There is a maybe, either it can be a value or not So we will handle maybe value with multiple values If we combine both of them What does it mean? You imagine for its power That you have a list of users. Let's say users, something like this. Now, all the users you have here, they may or may not have an email id. So, I may have an email id and I may not have an email id. What I have to do is, I have to print everyone's email ids on the screen. Right? Now, we have a traditional way of doing this. If you loop through these users, then you write, let's say, for every user you in users. Right? In a traditional way. And here you can check that if u.let's say getEmail is not equal to null, if the email exists in it, then you check one more if condition and then let's say there is a result list, result.add u.email Something like this. You can check this. And what will be the result? The result will be an empty list. We will fill it like this. This is a traditional method. You have seen this many times. But this is very ugly and verbose. We use Java's streams. If I know whether my email id can be or not. Then I will do one thing. I will make the field of email id in the user optional. So I will make it optional of string. We store email id in string format So I am saying that because email id can be or can't be So the email in users should return optional of string not direct string So that I can play with them properly Now see how I will use streams I will simply write my users.stream I converted it to stream Here I will put dot Now I can do here let's say map For every user u map it to u.getemail Now as soon as I call getEmail, let's say there is a method getEmail, it will return me optional of string, right? getEmail will not return me a string because I know it's a maybe or maybe not case. So it will return me optional of string. Now as soon as it returns optional of string, I have put a map on it. And you know what a stream map does? It returns you a stream. So basically it will return me a stream of optional of string. Something like this. So, this will be received. If I write it carefully, I can write that I will get a return here. Stream of Optional of String. Something like this Now because I have some stream of something written I can do dot filter on this something Because I can apply filter on stream So I can write here dot filter So I put filter here Now where do I put filter? You know I put filter in streams Now what is there in stream? Stream has wrapped an optional of string Now, if this is a stream of numbers like 1, 2, 3, 4, 5 and I apply a map on it. Forget about the operation, I just apply a map. So, what does the map return? It returns the stream of integer. like let's say stream of integer if I put a filter on it, then on what? on this stream of integer, basically on the values that are put on this integer so in the same way, the filter that I am putting here what is there in this stream? I am putting it on the optional of string on that optional of string, I can also do this by writing the condition inside let's say for every x map it to, now what is this x? this x is optional of string so every x map it to x dot is present is present. I wrote this condition. So if this condition, option of string is present or not, So basically, for every x, map it to x.isPresent. What is x? Optional of string. Is the value present in the optional of string? Yes or no. What is optional of string representing? It is representing this email id. So if it is present, then this filter will pass. Otherwise, the filter will not pass. If this present is true, then we will come ahead of this filter. And you know, this present just checks if there is value or not. It doesn't get us. So let's say I have to get it. So I will write dot map. And here again, for every x. In this case, what is x? Again, optional of string. I will write here x.get something like this So again I will get the email id Because again the filter will pass the same which will be present And finally I can write. toList Something like this Whatever I am getting, I can collect it in a list Something like this So my lambda expression will look something like this If you understand all the steps of this well Then you can make any number of lambda expressions Now see, first of all I converted the users to the stream. Before that, I assumed that this user class will have a method getEmail. Basically, it will be an email getter which will return me optional of string. It will not simply return string but will return optional of string because my value can be there or not. After that, as soon as I did map, how did I map it? For every u, u.get email. What did it return me? Optional of string. And again, I have mapped it. So, what is the total return type? Stream of optional of string. Okay. On that, I applied filter. Because I can apply on optional of string. And checked whether x is present. If it is present, then it will pass through filter. If it passes, then I applied map again. For every x, map it to x.get. Means, I fetched its inner string. And once I fetched it, then I put it in list. Something like this. Okay, I hope you understood well. You must have learned a lot. Now this video is very long. So let's keep this video as it is. If you have any question, then do ask in the comment section. And tell me how you liked the video. And please hype the video. See you in the next video. Till then, thank you so much.