In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you that Java's Optional can supplement all the relevant knowledge points of traditional if-null-else or if-not-null-else. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
The java.util.Optional added by Java is very popular and provides smoother code for methods that never return non-null values. Unfortunately, Optional has been abused, an abuse, and even overused. When null has no obvious advantage over using it directly, I occasionally come across code that uses Optional.
When Optional has no advantage over checking null directly, the calling code may prompt a red flag when it uses Optional.ofNullable (T) on the return value of the method it just called. As with all "danger signals", this does not mean that the method passes the return value as an Optional.ofNullable (T) (in fact, it is necessary to pass it to the API that expects the Optional), but it is usually used not to provide the actual value, rather than directly using the return value and checking whether it is null.
Before Optional is available, the code used to check the code returned by the null method and to take one way for the null response and another way for the non-null response is shown below).
/ * Demonstrates approach to conditional based on {@ code null} or * not {@ code null} that is traditional pre- {@ link Optional} approach. * / public void demonstrateWithoutOptional () {final Object returnObject = methodPotentiallyReturningNull (); if (returnObject = = null) {out.println ("The returned Object is null.");} else {out.println ("The returned object is NOT null:" + returnObject); / / code processing non-null return object goes here.}}
For this basic condition, there is little need to involve Optional. The next code snippet represents the type of code I occasionally see when developers try to replace explicit null detection with Optional:
/ * Demonstrates using {@ link Optional} in exactly the manner {@ code null} * is often used (conditional on whether the returned value is empty or * not versus on whether the returned value is {@ code null} or not). * / public void demonstrateOptionalUsedLikeNullUsed () {final Optional optionalReturn = Optional.ofNullable (methodPotentiallyReturningNull ()); if (optionalReturn.isEmpty ()) {out.println ("The returned Object is empty.");} else {out.println ("The returned Object is NOT empty:" + optionalReturn); / / code processing non-null return object goes here.}}
The paradigm in this code is basically the same as the traditional null check code, but the same check is performed using Optional.isEmpty (). This approach does not add any readability or other advantages, but does come at the expense of additional object instantiations and method calls.
A variant of the above usage, Optional, combines its ifPresent (Consumer) method with its isEmpty () method to form the same basic logic, doing one thing if the return value exists and doing another if the return value is empty. This is demonstrated in the following code.
/ * * Demonstrates using {@ link Optional} methods {@ link Optional#ifPresent (Consumer)} * and {@ link Optional#isEmpty ()} in similar manner to traditional condition based * on {@ code null} or not {@ code null}. * / public void demonstrateOptionalIfPresentAndIsEmpty () {final Optional optionalReturn = Optional.ofNullable (methodPotentiallyReturningNull ()); optionalReturn.ifPresent ((it)-> out.println ("The returned Object is NOT empty:" + it)); if (optionalReturn.isEmpty ()) {out.println ("The returned object is empty.");}}
This code looks a little shorter than the traditional method of directly checking the return value, but still at the cost of additional object instantiation and requires two method calls. In addition, it feels strange to first check if the Optional exists and then immediately check to see if it is empty. In addition, this approach becomes less practical if the logic that needs to be executed is more complex than writing messages to standard output.
These are all the contents of the article "can Java's Optional supplement all traditional if-null-else or if-not-null-else?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, 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.