Null Pointers are arguably the most common exception encountered by Java programmers, and a NullPointerException can be a surprise if the NullPointerException is not completely nulled. So there are often a lot of if null-statements in the program, resulting in the code looks very complex and bloated. Optional is a new feature introduced in Java 8 specifically designed to solve the void detection problem. Optional can be understood as a container that holds objects to be nulled. We can pull objects out of this container when we need to do something.

Optional structure

As you can see from the figure above, there is a variable value in Optional. This value is the object stored in the Optional container. The method in Optional operates on data that is value.

Create the Optional

There are two constructors in Optional, but both are private.

As you can see from the structure diagram above, there are three static methods in Optional: of, ofNullable, and Empty that return Optional

  • of; Receives a value argument that is not null

  • ofNullable; Accepts a possible value argument

  • empty: Creates a value of nullOptional

Example:

Optional<String> stringOptional = Optional.of("string");
Optional<Object> objectOptional = Optional.ofNullable(null);
Optional<Object> empty = Optional.empty();
Copy the code

Sentenced to empty

The Optional method of determining whether a value exists is isPresent and isEmpty

  • isPresent: returns true if the value exists

  • isEmpty: Returns true if null

The values

The Optional methods include get, orElse, orElseGet, and orElseThrow

  • get: An exception will be thrown if the value is null

  • orElse: receives an argument of the same type as value and returns it if value is null

  • orElseGet: Receives a value of the same type as valueSupplierParameter, when value is null,SupplierProvide the value of the

  • orElseThrow: Throws an exception if the value is null

orElseThrowThere’s an overloaded method, one that throws the default exception, and one that throws the specified exception and throws the default exception

Throws the specified exceptionSupplierprovide

Example:

consumption

  • ifPresent: Receive aA ConsumerArgument to perform a consumption operation when value is not null

Example:

  • ifPresentOrElse: Receives two parametersA ConsumerandRunnableIs executed when value is not nullConsumerThe consumption operation is performed when value is nullRunnableoperation

Example:

Conversion and filtering

The Optional conversion methods include map and flatMap, and the filtering methods include filter

  • map: One-to-one conversion Conversion value

mapMethod receives aFunctionType parameter,FunctionDefines a transformation operation

Example:

  • flatMap: One-to-many conversion value

flatMapThe method is similar tomapTo receive aFunctionType, the difference isflatMapThe receivedFunctionThe return value ofOptional

Example:

  • stream: convertstream

streamMethod is toOptionalPut itstreamIn the

  • filter: Checks whether value satisfies the judgment logic. If so, return to originalOptionalOtherwise return emptyOptional

Example:

Finally Optional is good, but don’t abuse it.