Java Optionals | Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to amigos code in this video I'm gonna show you exactly how to tackle no pointer exceptions with Java optionals I know in the past we all done something like this if a variable not equals to no then assign it to a value otherwise assign it to a default value but that's really a code smell to be honest and Java optionals allows us to eliminate all of that and allows us to write clean api's where any client looking at it knows that a specific field or variable might be nullable or not and the benefit of that is that we can then use functional programming and have clean code without further ado let's dive into it all right so I'm inside of IntelliJ and let's go ahead and learn about optionals so to use optionals we simply use optional and then dot and right here you can see that we have few methods so we have optional dot empty and then optional off so of it means that you for sure know that the value is not no and then you also have optional of and then nullable so this is where you're not sure whether the value will be present so therefore you can use optional or knowable so if I go ahead and use optional of empty and extract this to a variables you can see the keyboard shortcut down below so if I didn't sound this so empty if I run there you see that we get an optional of empty right here now we have few methods inside of the optional class and we can find out few things so we can go ahead and say is present so this tells us whether there is a value inside of that optional we can also say the universal is empty right so if I run this you can see that we get false so it's not present and then we which for is empty now if I go ahead and use optional of and then simply say hello right and then this will be an optional string and then if I execute this and now you can see that is present is true and then is empty is false so let's go ahead and explore some other methods so if I say empty and then dot and then you can see that we have map and then if President or else is empty flat map get or else so let's go ahead and use or else so or and then else so or else so this is actually a default value when the value inside of the optional is not present so hello world so right here let me go ahead and extract this to a variable so or and then else now if I south so if I system dot print line and then or else and run this you can see that we get hello and this is because this empty optional is in fact not empty right so let me actually rename this to hello like that and yeah so ello is not empty so if I go ahead and personal and remember I said that optional of no so this will not work because I'm expecting this to have a value if I use optional of so if I run this I just wanted to see the actual error so we get no pointer exception so when you not sure whether the value will be inaudible or not you can use optional of and then nullable now if I run this you can see that hello was empty right so was empty so right here so this line returns true and it's present returns false so it's not present and then what we did here was we said hello and then or else so if ello is not present then we get hello world so usually if you were not using optionals you would say you know string and then lo and then equals to no and then it's a shrink and then world and then basically you would say if and then something like this hello no equals to now and then basically you would you know assign and you can see that this can become really messy nowadays this is too much imperative programming and basically what we want to do is just use declarative programming because there's no way for us to do this code where we have you know the benefits of optionals so then what we could do is use functional programming right because now I do have an option of chaining these methods so right here you see that I said or else but I could actually before or else I could use dot and then map right so map allows me to do a transformation on the actual value inside of the optional in case it's present so if I go ahead and say string and then this will be 2 and then uppercase and right here let's go ahead and pass hello all lowercase if I run this you can see that we get hello and then you know uppercase and because it's present this time we don't get the world part now we could also instead of saying or else we could say or else and then get right here so or else get so this allows us to pass a runnable and inside of oh actually a supplier right so a supplier and inside of this supplier we can have any logic to get a default value so you saw that with our else you simply pass a stream but here you can perform some extra computations I want to leave a comment here so extra computation to retrieve the value and then at the end what you need to do is return the value that you've computed so right here what I'm going to do is simply you know say a world but then right so you know you could imagine that world for example was in the database so inside here you could actually say you know go to the database and then French the world part in case ello is not there something like that so if I run this you see that I would still get low but because this guy is not empty so if I pass null inside and now you can see that we get world so let's go ahead and learn about some other methods so we could use dot and then or right so or it's pretty much as or else but you can see that we get the option of returning and optional as well right so or else right here with that we return a string but with all we can return an optional so we could also say or else throw so we could say for example throw an illegal state exception and you could have a custom message there but basically now if I run this you can see that it throws an illegal set exception so this is often when you are for sure that they should be a value in there and for some reason it wasn't so then you throw an illegal stay exception so let's look at some other methods so dot and then we could use if present or else or if present so let's go ahead and use if present so if present so this guy here takes the actual value inside of your optional if it's present right so let me go ahead and say simply this is word and then inside so right now we no longer basically we no longer getting a string out of it but essentially if I say hello and then you see that if it's present then we get the word so in our case this will be alone or actually used in a second but if I pass in our word and if I remove that if I run this you see that if I then this properly you see that we get nothing and this is because it's no but if I pass a low and then I run this you can see now we get world so it's really cool and we can even change this to a method reference we could also so if I delete that we could also say if present or else right so this is pretty much almost the same so this is word right so we take the word here and then we're going to do the same thing so salt and then word but then the second argument is a runnable right so this is the or else part so this we could simply say South as well and then world and if I change this to my for reference like that and I can change this so online and there we go so if I run this you see that we get a low but if I pass no inside you see that we get world so this is pretty much the basics of using optionals now when I said that you could have an API where a client may look at it and know that I specific variable might be know is by using optionals inside of your classes or pojos returning methods and pretty much wherever you think that a class or a value right might be know so as an example we could do something like this so if I remove everything from here so what we could do is for example so let's go outside here and create class right and say that this is person so we know for sure that every person so let's go ahead and say private final and then string and then name right so we know that for sure every person has a name but we're not sure really if everyone has an email right so it's officially first a bad way of doing it and then I'm going to show you how to fix it with optionals so add that to constructor and then add togethers both for them and then if we go ahead and create a new person so new person and this will be James and then the email will be James at gmail.com and then let's call this person right so now I could go ahead and say South and then person dot and then get and then email and let's say that we want to lowercase this guy right so in fact just let me put all uppercase here so James like that now I can run this and you see that this works right but now if I pass no inside so no run this you see that we gain no pointer exception so one way to fix this is to let the client know that emails might be nullable so what we could do is wrap this inside of optionals so just like that so the getter and the return will be optional and then dot of nullable so this is what you've seen before so now right here what we could do is pretty much just use dot and then map so we can map a string and then to lowercase and then or else or actually or else and then email not provided something like this provided so we could actually use the person you know get name and then say that you know James hasn't got an email for example but if I run this and in fact just let me extract this to it very well so you can see what we're doing so just like that email and then put this like that and then like that and a space here and another one here so basically now you see that email is not provided and if I pass James so James shot and then gmail.com or this could even be uppercase and then run this and you see that the email is lowercase and finally one more thing that I forgot to show you is you can use the if present and then unwrap the actual value inside of the optional yourself so if I common all of this so you could do the same thing as by saying if and then person dot get email is present right so now you know that the email is present so now to get the actual email you can say string and then email equals to person dot email watch to get email and then dot and then get so basically now you are getting the value inside of the optional and now you could say South and then email dot to lowercase right and then you could have an else branch and then South and then email not provided so just let me copy this here and then paste it here and if I run this you see that we get james at gmail.com all lowercase like if I pass no so basically the same thing so run you see that we get email not provided I just want to show you that you can unwrapped optionals yourself and this is how you use Java optionals as you see it's very powerful you can take full advantage by introducing it in your codebase and eliminate where no values are likely to be present if you have any questions go ahead and comment down below otherwise make sure to subscribe to my channel to get more videos like this also follow me on my Instagram where the community is growing and join me in the next one see ya
Info
Channel: Amigoscode
Views: 51,046
Rating: undefined out of 5
Keywords: java, java optionals, java functional programming, how to use java optionals, what is null pointer exception, java11, dependency injection, streams api, java 8 streams example, flatmap example in java 8, flatmap example, flatmap usage in java, flat map tutorial, optional tutorial, optional in java, optional in java 8, optional usage in java, functional programming in java, java download
Id: 1xCxoOuDZuU
Channel Id: undefined
Length: 15min 57sec (957 seconds)
Published: Fri Jan 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.