The new Java8 feature provides a container class that can hold NULL, which can be used to solve null pointer exceptions and other situations.

Introduction to the

The Optional class is a container class that can hold either the value of template type T or null.

If the value exists, isPresent() returns true, and get() returns the object stored in the container. This way we do not need to do null-pointer exceptions alone, and it is convenient for functional programming.

What’s the use?

Think about it: why is null-pointer exception resolved?

Before Java8, you needed to verify that an object was null when called, which would most likely result in null-pointer exceptions:

String name = student.getInfo().getName();
Copy the code

To avoid throwing an exception, you need to validate:

{...if(student! =null){
	Info info = student.getInfo();
	if(info! =null){
		String name = info.getName();
		returnname; }}... }Copy the code

Too much trouble!!

The Optional class introduced in JDK8 makes things easier.

Don’t believe it? Show you the code:

{... Student student =Student().builder()...... build(); Optional<Student> studentOp=Optional.ofNullable(student); Info info =newInfo(); studentOp.ifPresent(stu->{info =stu.getInfo(); }; . }Copy the code

As shown above, Optional provides a neat way to avoid null-pointer exceptions and get container class values elegantly, as well as support for lambda functional programming.

Other methods

Let’s take a look at Optional’s other apis.

Of and ofNull

An Optional. Of () operation can only accept non-null arguments, otherwise a null pointer exception will be reported.

OfNullable () has no such limitation, and both null and non-null can be accepted, depending on the actual situation:

Student student=null;

/ / complains
Optional<Student> stuOp=Optional.of(student); 
// No error will be reported
Optional<Student> stuOp=Optional.ofNullable(student); 
Copy the code

OfNullable can receive null values, so use the following API to verify that such null values are allowed.

IsPresent, ifPresent

IsPresent () is used to verify that the value in the Optional instance has a value. If it is null, it returns false. IsPresent is the same as an if statement for the object in the Optional container.

IfPresent () is a functional programming interface: if Optional is not null, the consumer function is called. Consumer refers to an abstract method that inherits from the Consumer class and can work with incoming data, as well as directly using lambda expressions.

Of the form above:

studentOp.ifPresent((stu)->{info =stu.getInfo()});
Copy the code

If the Student object in studentOp is not null, that object is treated as stU, simplifying the program considerably.

get

This method is used to return the value in Optional. If null, an exception is thrown: NoSuchElementException,

OrElse and orElseGet

OrElse is used to return the value in the Optional instance or, if null, the given value. Such as:

studentOp.orElse(println("hello!"));
Copy the code

OrElseGet is a little different. OrElseGet supports custom output using lambda expressions.

map

The map method performs continuous operations on Optional instances. If Optional has a value, the mapping function returns the value. If the return value is not null, the Optional value containing the value returned by the mapping function is returned. Otherwise return empty Optional.

Map functions can use lambda expressions. The map function can be extremely useful, and can be used with orElse or ifPresent for cool functional programming. Here’s a quick example:

. studentOp.... prtintln(studentOp.map((info)->info.setName("I'm xiaoming")).orElse("I'm King!"));
Copy the code