In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, Xiaobian will bring you about how to use java.util.Optional in Java 8. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
Optional is mainly used to determine whether it is empty. Before Optional, we only need to use the StringUtils tool class to determine whether the passed parameter is empty. Now we can use Optional instead.
Optional.of(Object object)
Optional.of(Object object): The input parameter object cannot be null, otherwise a null pointer exception will be thrown. It ends up calling the Objects.requireNonNull method.
1
2
3
4
5
public static T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
Optional.ofNullable(Object object)
Optional.ofNullable(Object object): The input object can be empty. If the object is not empty, an Optional instance is created; if the object is empty, an Option object with static facade is returned. Note that there will be no new Option instance here, but a static final instance EMPTY is used. What is interesting here is the generic problem. For example, I need two Optional objects, the types are String and Integer respectively.
1
2
Optional optionalStr = Optional.ofNullable(null);
Optional optionalInt = Optional.ofNullable(null);
How can we guarantee that the same object will be returned if the type is different? Look directly at ofNullable's source code and find that the empty method will be called:
1
2
3
4
5
public static Optional empty() {
@SuppressWarnings("unchecked")
Optional t = (Optional) EMPTY;
return t;
}
It turns out to be implemented through coercion, and then look at the EMPTY object:
1
private static final Optional EMPTY = new Optional();
Optional.empty()
Optional.empty(): is the empty method used when analyzing Optional.ofNullable above, directly returning a static final instance EMPTY;
Optional.of() method is used a bit like assertion. When the object is empty, it represents an unacceptable exception in the business, which needs to be handled as soon as possible, and the business refuses to execute. In this scenario, Optional.of can be used.
Let's look at an example.
User entity class codes are as follows:
1
2
3
4
5
6
7
8
9
10
11
public class User {
private int id;
private String name;
private int age;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
Optional is used to avoid the null exception problem.
1
2
3
4
5
6
public User getStudent(int id){
User user = null;
Optional optional = Optional.ofNullable(user);
return optional.orElse(DEFAULT);
}
orElse method can specify a return object when the value is empty. If this object needs to be obtained by calling the method (for example, we can't get the DEFAULT object, we have to get it through getDefault() method), orElseGet method is needed to achieve the purpose, as follows:
1
2
3
4
5
6
7
8
9
private User getDefault(){
return DEFAULT;
}
public User getStudent(int id){
User user = null;
Optional optional = Optional.ofNullable(user);
return optional.orElseGet(() -> getDefault());
}
The above is how java.util.Optional is used in Java 8 shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.