Table of contents
I. Overview
Null pointer exceptions occur the most when we are coding, so in many cases we need to make various non-null judgments.
Especially when the properties in the object are still an object, this kind of judgment will make the code look bloated
Therefore, Optional was introduced in JDK8, and more elegant code can be written to avoid null pointer exceptions.
And Optional is also used in many functional programming-related APIs. If you don't know how to use Optional, it will also affect the learning of functional programming.
Two, use
1. Create an object
Optional is like a wrapper class, which can encapsulate our specific data inside the Optional object. Then we use the methods encapsulated in Optional to manipulate the encapsulated data to avoid null pointer exceptions very elegantly.
We generally use Optional 's static method ofNullable to encapsulate data into an Optional object. No matter whether the incoming parameter is null or not, there will be no problem
You may find it cumbersome to add a line of code to encapsulate the data. But if we modify the getAuthor method so that its return value is the encapsulated Optional, it will be much more convenient for us to use.
And in actual development, a lot of our data is obtained from the database. MyBatis can and already supports Optional from version 3.5. We can directly define the return value type of the dao method as an Optional type, and MyBastis will encapsulate the data into an Optional object by itself. return. The process of encapsulation does not require our own operation.
If you are sure that an object is not empty, you can use the optional static method of to encapsulate the data into an Optional object
But it must be noted that if you use of, the parameters passed in must not be null. (Try to pass in null and what will happen)
If the return value type of a method is optional type. And if we judge and find that the return value of a certain calculation is null, then we need to encapsulate nul into an optional object and return it. At this time, you can use the optional static method empty to encapsulate
aptiona1.empty()
2. Safe consumption value
After we get an Optiona object, we definitely need to use the data in it. At this time, we can use its ifPresent method pair to consume the value. This method will judge whether the data encapsulated in it is empty, and only when it is not empty will the specific consumption code be executed. This is much safer to use. For example, the following writing method elegantly avoids the null pointer exception
3. Get the value safely
If we expect to get the value safely, it is not recommended to use Optional's get, but the following method
ofElseGet: Get the data and set the default value of the data to be empty. If the data is not empty, get the data value. If it is empty, create an object according to the parameters you pass in as the default value and return
ofElseThrow: If the data is not empty, get the data, if it is empty, create an exception and throw it according to the parameters you pass in
4. Filter
We can use the filter method to filter the data. If there is data originally, but it does not meet the judgment, it will become an optional with no data.
5, judgment
You can use the isPresent method to judge whether there is data. If it is empty, it returns false, and if there is data, it returns true. It is more recommended to use the ifPresent method
6. Data conversion
The map method allows us to convert data, and the obtained data is packaged by Optional, which is safe to use