In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "Java8 how to achieve empty", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve Java8 empty" this article.
Introduction
A null check is often encountered in the development process, and a NullPointerException exception will be generated if the null check is not done. It may be problematic to use the following code directly.
User.getAddress () .getProvince ()
When user is null, a null pointer exception is generated. The general solution is as follows:
If (username null) {Address address = user.getAddress (); if (addressworthy null) {String province = address.getProvince ();}}
This kind of writing is relatively ugly, in order to avoid the above ugly writing, let the ugly design become elegant. Java8 provides the Optional class to optimize this writing.
API introduction
First introduce API, unlike other articles, this article takes an analogical approach, combined with the source code. Unlike other articles, API is listed one by one, so that people can not find the main points.
1. Optional (), empty (), of (), ofNullable ()
There is a correlation between these four functions, so they are put in a group for memory.
Just to be clear, Optional (T value), that is, the constructor, is private-authorized and cannot be called externally. The remaining three functions are public permissions for us to call. Then, the essence of Optional is that a real value is stored internally, and when it is constructed, it is directly determined whether the value is empty or not. Well, that's still a little abstract. Go directly to the source code of the Optional (T value) constructor, as shown in the following figure
So, the source code for of (T value) is as follows
Public static Optional of (T value) {return new Optional (value);}
In other words, the constructor is called inside the of (T value) function. From the source code of the constructor, we can draw two conclusions:
The Optional object constructed by the of (T value) function will still report NullPointerException when the value is empty.
The Optional object constructed by of (T value) function can construct Optional object normally when the Value value is not empty.
In addition, the Optional class also maintains an object with value as null, which probably looks like the following
Public final class Optional {/ / omitted. Private static final Optional EMPTY = new Optional (); private Optional () {this.value = null;} / / omitted. Public static Optional empty () {@ SuppressWarnings ("unchecked") Optional t = (Optional) EMPTY; return t;}}
The purpose of empty (), then, is to return the EMPTY object. Well, after laying the groundwork, it can be said that ofNullable (T value) plays a role, on the source code.
Public static Optional ofNullable (T value) {return value = = null? Empty (): of (value);}
Well, everyone should be able to understand what it means. Compared with of (T value), the difference is:
When the value value is null, of (T value) reports a NullPointerException exception
OfNullable (T value) does not throw Exception,ofNullable (T value) directly return an EMPTY object.
Does that mean that we only use the ofNullable function instead of the of function in the project?
No, a thing exists so naturally it has the value of existence. When running, you don't want to hide NullPointerException. Instead, report it immediately, in which case the Of function is used. But I have to admit, such scenes are really rare. Bloggers have only used this function when writing junit test cases.
2. OrElse (), orElseGet () and orElseThrow ()
These three functions are memorized in groups and are called when the value passed in by the constructor is null. The use of orElse and orElseGet is shown below, which is equivalent to giving a default value when the value value is null:
@ Testpublic void test () {User user = null; user = Optional.ofNullable (user) .orElse (createUser ()); user = Optional.ofNullable (user) .orElseGet (()-> createUser ());} public User createUser () {User user = new User (); user.setName ("zhangsan"); return user;}
The difference between the two functions: when the user value is not null, the orElse function will still execute the createUser () method, while the orElseGet function will not execute the createUser () method, so you can test it yourself.
As for orElseThrow, when the value is null, an exception is thrown directly. The usage is as follows:
User user = null;Optional.ofNullable (user). OrElseThrow (()-> new Exception ("user does not exist"); 3, map () and flatMap ()
These two functions are put in a set of memories, and these two functions do the operation of converting values.
Directly on the source code
Public final class Optional {/ / omitted. Public Optional map (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.