In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the role of Optinal, the new feature of java8?". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the role of Optinal, the new feature of java8?"
Null pointer exceptions may account for half of the exception rate. So oracle wants to use 'elegant' like optional to solve null. The imagination is beautiful, but the reality is cruel. Obviously optional doesn't make the code that elegant. After reading seven or eight articles on God's blog, no matter there is no qualitative change in terms of code quantity or code thought, you still have to worry about the emergence of null and deal with it accordingly. If you want to handle it uniformly, you might as well directly code try catch null pointer exception. Until I find that it really improves the code, I don't recommend it for the time being (but Ali recommends it, it's still awesome, listen to Ali)
Update-
In the spirit of seeking truth and pragmatism, let's analyze the use of optional in more detail today. (according to the actual development, theoretical players ignore it).
1. The first is the construction methods Optional.of (), Optional.ofNullable (), Optional.empty ().
Optional.of (): the of method throws a null pointer exception when the parameter passed in is null. Many bloggers say they want to use it when determining that the parameter is not null. But if it is determined that the parameter is not null, why use Optional. From a personal point of view, all the methods of Optional that may throw null pointer exceptions go against the original intention of using Optional. Such as the get () method.
Optional.empty (): construct a null value optional, which is of no use in non-special cases. It is constructed using the ofNullable () method by default.
2, the use of each method
① isPresent () returns boolean based on whether it is empty or not. A method of digging and filling holes. Direct judgment parameter = = null? Isn't it good.
② ifPresent (Consumer) executes Consumer (function expression) when the object is not empty, and space-time does not execute. It is equivalent to if (obj==null) {.}. You might say it's not of much use. My hard drive doesn't care about an extra line of code. I can't even write an else. This... You're right. But this is already a useful method in Optional.
③ get () gets the value in optional, while null throws an exception. As mentioned earlier, this method does not show the value of use.
If ④ orElse (T other) is empty, replace it with other. You can also replace it with a single line of if. But you can use it to install.
⑤ orElseThrow throws an exception if it is empty. Don't want to judge it.
⑥ map if you want to get sub-objects of the Ruogan layer from an object, each layer should have a null check such as Obj=obj1.get ("obj2"). Get ("obj3"). Get ("obj4"); when the value returned by the previous get method is null, the next get method will throw an exception when it is executed. So you have to write several layers of if. Map allows chained operations, dealing with null values at the end. This is the most worthwhile method for optional.
Optional.ofNullable (userEntity1) .map (u-> u.getUsername ()) .map (name-> name.toUpperCase ()) .orElse (null); at this point, I believe you have a better understanding of "what the new java8 feature Optinal does", so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.