Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Optional class of Java8

Shulou Source: shulou.com Published: 2022-06-01 13:18:15 09月18日 Update

This article mainly introduces "how to use the Optional class of Java8". In the daily operation, I believe that many people have doubts about how to use the Optional class of Java8. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to use the Optional class of Java8". Next, please follow the editor to study!

Detailed explanation of the use of Optional 1. Introduction to Optional

The Optional class is a container object that can be null. If the value exists, the isPresent () method returns true, and a call to the get () method returns the object.

Optional is a container: it can hold the value of type T, or just null. Optional provides a lot of useful methods so that we don't have to explicitly detect null values.

The introduction of Optional class is a good solution to null pointer exceptions.

2. Build Optional

Build an Optional object; the methods are: empty (), of (), ofNullable ()

/ / returns an Optional that describes the given value, or an empty Optional if it is not null. / / Parameter: value-description may be a null value / / Type parameter:-Type of value / / return value: an Optional and if the specified value is a non-current value null, otherwise an empty Optional Optional.ofNullable ("); / / returns an Optional description of a given non-null value. / / Parameter: value-the value to be described must be a non-null / / type parameter:-the type of the value / / return value: Optional Optional.of ("") of the value exists; / / returns an empty Optional instance. This Optional has no value. / / Type parameter:-Type of non-existent value / / return value: an empty Optional / / api Note: although this may be tempting, you should avoid testing whether the object is empty by comparing = = with the instance returned by Optional.empty (). / / there is no guarantee that it is a singleton. / / instead, use isPresent () Optional.empty (); 3, Optional API and source code comment package java.util;import java.util.function.Consumer;import java.util.function.Function;import java.util.function.Predicate;import java.util.function.Supplier;import java.util.stream.Stream;/**, a container object that may or may not contain non-null values. If a value exists, isPresent () returns true. If no value exists, the object is considered empty and isPresent () returns false. Other methods are provided that depend on whether the contained value exists, such as orElse () (returning the default value if no value exists) and ifPresent () (performing an action if one exists). This is a value-based class; using identity-sensitive operations on Optional instances (including reference equals (= =), identity hash codes, or synchronization) can produce unpredictable results, and you should avoid using * / public final class Optional {/ * empty () generic instance * / private static final Optional EMPTY = new Optional (); / * if not empty, this value; otherwise, false. If null, it means that there is no value * / private final T value; / * * to construct an empty instance. Impl Note: in general, there should be only one empty instance per VM EMPTY * / private Optional () {this.value = null;} / * * returns an empty Optional instance. This Optional has no value. Type parameter:-Type return value of non-existent value: an empty Optional api Note: while this may be tempting, you should avoid testing whether the object is empty by comparing = = with the instance returned by Optional.empty (). There is no guarantee that it is a singleton. Instead, use isPresent () * / public static Optional empty () {@ SuppressWarnings ("unchecked") Optional t = (Optional) EMPTY; return t;} / * * to construct an instance using the values described. Parameter: value-non-null value to be described throws: NullPointerException if the value is null * / private Optional (T value) {this.value = Objects.requireNonNull (value);} / * * returns a non-null value given by the Optional description. Parameter: value-the value to be described must be a parameter of non-null type: the type of the value returns a value: Optiona * / public static Optional of (T value) {return new Optional (value);} / * * returns an Optional describing the given value, or an empty Optional if it is not null. Parameter: value-description may be null value type parameter:-value type return value: an Optional and if the specified value is not the current value null, otherwise an empty Optional * / public static Optional ofNullable (T value) {return value = = null? Empty (): of (value);} / * * returns the value if it exists, otherwise throws a NoSuchElementException. Return value: the non-null value described by this Optional throws: NoSuchElementException if there is no value api Note: the preferred alternative to this method is orElseThrow (). * / public T get () {if (value = = null) {throw new NoSuchElementException ("No value present");} return value;} / * * returns true if there is a value, false otherwise. Return value: true if there is a value, false * / public boolean isPresent () {return value! = null;} / * * if there is no value, true, otherwise false. Return value: true if no value exists, otherwise false * / public boolean isEmpty () {return value = = null;} / * * if there is a value, the given operation is performed with this value, otherwise no action is performed. Parameter: action-Action to be performed (if there is a value) * / public void ifPresent (Consumer

Tags: Parameters types functions punks objects instances results methods supplies employees bonuses predicates writing functions actions learning applications testing ugliness containers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Shulou Technology Shulou Tech Info MySQL