In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to avoid writing a large number of null statements in Java code related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this article on how to avoid writing a large number of null statements in Java code will have a harvest, let's take a look at it.
Some people say that you can use the Optional provided by JDK8 to avoid null, but it is still a bit troublesome to use.
In the daily work, the author encapsulates a tool that can chain call object members without judging null, which is more elegant and easy to use than the original if null logic and Optional provided by JDK8. It not only greatly improves the coding efficiency in engineering practice, but also makes the code more accurate and elegant.
Unelegant empty call
I think my friends who are engaged in Java development must have encountered the following uncomfortable empty logic: now there is a User class of which School is a member variable.
/ * * @ author Axin* @ since 2020-09-20 * @ summary A User class definition * (Ps:Data is an annotation provided by the lombok component, simplifying the convention code for get set, etc.) * / @ Datapublic class User {private String name; private String gender; private School school; @ Datapublic static class School {private String scName; private String adress;}
Now if you want to get the member variable adress of School, the general way to handle it is:
Public static void main (String [] args) {User axin = new User (); User.School school = new User.School (); axin.setName ("hello"); if (Objects.nonNull (axin) & & Objects.nonNull (axin.getSchool () {User.School userSc = axin.getSchool (); System.out.println (userSc.getAdress ());}}
Although it is troublesome to check the School null when getting the adress, it is also possible to use the Optional tool provided by JDK8, but there is still some trouble.
The following OptionalBean provides a way to chain-call member variables without nulling, directly to the target variable you want to get without worrying about null pointers.
Chain call member variable
If you use the tool OptionalBean designed in this article, the above call can be simplified like this:
Public static void main (String [] args) {User axin = new User (); User.School school = new User.School (); axin.setName ("hello"); / / 1. Basic call String value1 = OptionalBean.ofNullable (axin) .getBean (User::getSchool) .getBean (User.School::getAdress). Get (); System.out.println (value1);}
Execution result:
Where the school variable of User is null, you can see that the code does not have a null pointer, but returns null. How does this tool work?
The OptionalBean tool / * * @ author Axin* @ since 2020-09-10 * @ summary chain calls the value method in bean * / public final class OptionalBean {private static final OptionalBean EMPTY = new OptionalBean (); private final T value; private OptionalBean () {this.value = null A null value throws a null pointer * @ param value * / private OptionalBean (T value) {this.value = Objects.requireNonNull (value);} / * wraps a bean * @ param value * @ param * @ return * / public static OptionalBean of (T value) {return new OptionalBean (value) that cannot be empty. } / * wrap a possibly empty bean * @ param value * @ param * @ return * / public static OptionalBean ofNullable (T value) {return value = = null? Empty (): of (value);} / * * take out the specific value * @ param fn * @ param * @ return * / public T get () {return Objects.isNull (value)? Null: value;} / * take out an object that may be empty * @ param fn * @ param * @ return * / public OptionalBean getBean (Function
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.