New features of Java 8 Optional

New features of Java 8 Optional

Brief introduction of Optional

Function: reduce the judgment of null value
Logic: when a variable exists, the Optional class simply encapsulates the class; when it does not exist, the missing value will be modeled as an "empty" Optional object returned by the method Optional.empty()

Optional statement

//Define an entity
@Data //Annotation in lombok, including @ Getter,@Setter
public class Car {
	private int id;
	private String name;
	private String createDate;
	private double price;
}

//1. Declare an empty Optional object
 Optional<Car> optCar = Optional.empty();

//2. Create Optional based on a non null value
Car car = new Car();
car.setCreateDate("2019-03-27 12:12:12");
car.setPrice(188888.88);
Optional<Car> optCar = Optional.of(car);

//3. Declare an Optional that can accept null (allow null value, create null object)
Car car = new Car();
Optional<Car> optCar = Optional.ofNullable(car);

Common methods of Optional

// 1.map() method -- map
Optional<Car> optCar = Optional.ofNullable(car);
Optional<String> optStr = optCar.map(Car :: getCreateDate);

2.flatMap() method - simplify the non empty judgment of multiple member variables of an object

car.getId().getPrice().getPrice();

//2.1 judgment and use method of optional
Optional<Car> optCar = Optional.of(car);
optCar.flatMap(Car :: getName).flatMap(Car :: getPrice).flatMap(Car :: getCreateDate).orElse("unKnown");

//2.2 usage of objects
Optional<Car> optCar = Optional.of(car);
optCar.flatMap(Car :: Engine).map(Engine :: getName).orElse("unKnown");

3. Summary of methods for obtaining values of optional

//3.1 get() method, if any, returns the encapsulated variable value, otherwise, a NoSuchElementException is thrown
Car carNew = optCar.get();

//3.2 orElse() method, which has been used before, provides a default value when the object is not included
optCar.flatMap(Car :: Engine).map(Engine :: getName).orElse("unKnown");

//3.3 delay call Supplier for orelseget (Supplier <? Extends x > Ex) and orElse, only when the Optional object does not contain a value
Car carNew = optCar.orElseGet(()->null); //Used alone is to return the encapsulated type, which is filled with null when the property does not exist
String name = optCar.map(Car :: getName).orElseGet(()->null); //The returned object is the map mapped object

//3.4 orelsethrow (supplier <? Extends x > Ex) is similar to get() method. When it is empty, custom exception can be thrown
Car car1 = optCar.orElseThrow(()->new RunTimeException());

//3.5 when ifpresent (consumer <? Super T > Ex) variable exists, execute a parameter passing in method, otherwise no operation will be performed
optCar.ifPresent(car2->System.out.println(car2.getName()));

//3.6 isPresent() method to determine whether there is a value
boolean flag = optCar.isPresent();

4.filter() method, filtering

optCar.filter(value->value.getPrice>1000000); //If not, an empty Optional object trueOptional.empty will be returned. Otherwise, trueOptional[Car {...}] will be returned

Tags: Java Lombok

Posted on Sun, 01 Dec 2019 08:25:10 -0800 by krraleigh