In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the new features of java8". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the new features of java8"?
As a java developer, everyone has the problem of calling a method to get the return value but not directly using the return value as a parameter to call another method. We first need to determine whether the return value is null, which can only be used as a parameter for other methods if it is not empty. Otherwise, there will be a NPE exception, the legendary null pointer exception.
For example:
If (null = = str) {/ / null pointer determines return 0;} return str.length (); / / uses optionalreturn Optional.ofNullable (str) .map (String::length) .orElse (0) / / A more complicated public String isCheckUser (User user) {if (null! = user) {/ / get the role if (null! = user.getRole ()) {/ / get administrator privileges if (null! = user.getRole (). GetPermission ()) {return "get administrator privileges";} else {return "user is empty" }} / / use optional class public String isCheckUser (User user) {return Optional.ofNullable (user) .map (u-> u.getRole) .map (p-> p.getPermission ()) .orElse ("user is empty");}
This article will analyze step by step according to the new java8 feature Optional class source code and teach you how to use the Optional class to elegantly determine whether it is null.
The composition of the optional class is as follows:
We know from the comments on the class that this is a container object that can be null or not null. If the value exists, the isPresent () method returns true, and a call to the get () method returns the object.
Next, we will explore the methods in the Optional class one by one, and give examples.
Of
Source code: / * Returns an {@ code Optional} with the specified present non-null value. * * @ param the class of the value * @ param value the value to be present, which must be non-null * @ return an {@ code Optional} with the value present * @ throws NullPointerException if value is null * / public static Optional of (T value) {return new Optional (value);}
As you can see from the source code comments, this method returns an optional object that does not null, and throws a NullPointerException exception if the value is null.
Examples are as follows:
/ / call factory method to create Optional instance Optional name = Optional.of ("javaHuang"); / / pass in parameter null and throw NullPointerException.Optional nullValue= Optional.of (null)
OfNullable
Source code: / * Returns an {@ code Optional} describing the specified value, if non-null, * otherwise returns an empty {@ code Optional}. * * @ param the class of the value * @ param value the possibly-null value to describe * @ return an {@ code Optional} with a present value if the specified value * is non-null, otherwise an empty {@ code Optional} * / public static Optional ofNullable (T value) {return value = = null? Empty (): of (value);}
You can know from the comments that an optional object is returned when value is not null, and an empty optional object is returned if value is null.
Examples are as follows:
/ / returns empty optional object Optional emptyValue = Optional.ofNullable (null); or// returns optional object Optional name= Optional.ofNullable ("javaHuang") with name of "javaHuang"
IsPresent
Source code: / * Return {@ code true} if there is a value present, otherwise {@ code false}. * * @ return {@ code true} if there is a value present, otherwise {@ code false} * / public boolean isPresent () {return value! = null;}
You can know from the comment that if the value is null, false is returned, and true is not returned for null.
Examples are as follows:
If (name.isPresent ()) {System.out.println (name.get ()); / / output javaHuang} emptyValue.isPresent () = = false
Get
Source code: / * If a value is present in this {@ code Optional}, returns the value, * otherwise throws {@ code NoSuchElementException}. * * @ return the non-null value held by this {@ code Optional} * @ throws NoSuchElementException if there is no value present * * @ see Optional#isPresent () * / public T get () {if (value = = null) {throw new NoSuchElementException ("No value present");} return value;}
You can know from the comments: if value is not null, return an optional object, and if value is null, throw a NoSuchElementException exception
Examples are as follows:
Try {System.out.println (emptyValue.get ());} catch (NoSuchElementException ex) {System.err.println (ex.getMessage ());}
IfPresent
Source code: / * If a value is present, invoke the specified consumer with the value, * otherwise do nothing. * * @ param consumer block to be executed if a value is present * @ throws NullPointerException if value is present and {@ code consumer} is * null * / public void ifPresent (Consumer
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.