本文介绍一下Optional在实际开发应用中的使用情况
一、概述
官方API-Optional。
Optional是JKD1.8发布的一个类库,主要是解决烦人的NullPointerException问题以及if、else,提升代码简洁度。
二、应用
ofNullable
static <T> Optional<T> ofNullable(T value)
返回一个T类型的Optional对象
String string = "123";
Optional<String> stringOptional = Optional.ofNullable(string);
//输出:Optional[123]
ifPresent
void ifPresent(Consumer<? super T> consumer)
String str2 = "二哈";
Optional.ofNullable(str2).ifPresent(var -> {
System.out.println(var);
});
//输出:二哈
//等同于:
if (Objects.nonNull(str2)) {
System.out.println(str2);
}
orElse
T orElse(T other)
String str2 = "二哈";
String result = Optional.ofNullable(str2).orElse("大哈");
System.out.println(result);
//输出:二哈
//等同于:
if (Objects.nonNull(str2)) {
System.out.println(str2);
} else {
System.out.println("大哈");
}
orElseGet
T orElseGet(Supplier<? extends T> other)
String str3 = null;
String result = Optional.ofNullable(str3).orElseGet(() -> {
return "233";
});
System.out.println(result);
//输出:233
//等同于:
String str3 = null;
if (str3 == null) {
return "233";
} else {
return str3;
}
orElseThrow
<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
String str4 = "233";
String result2 = Optional.ofNullable(str4).orElseThrow(RuntimeException::new);
System.out.println(result2);
//输出:233
//等同于:
if (str4 != null) {
System.out.println(str4);
} else {
throw new RuntimeException();
}
map
<U> Optional<U> map(Function<? super T, ? extends U> mapper)
@Builder
@Data
public class Animal {
//猫
private Cat cat;
//狗
private Dog dog;
}
@Builder
@Data
public class Dog {
//种类
private String species;
private Action action;
}
@Data
@Builder
public class Cat {
//种类
private String species;
private Action action;
}
@Data
@Builder
public class Action {
private String eat;
private String sleep;
}
public class TestMain {
public static void main(String[] args) {
Action dogAction = Action.builder().eat("我喜欢吃狗粮").sleep("喜欢睡觉").build();
Action catAction = Action.builder().eat("我喜欢吃猫粮").sleep("喜欢睡觉").build();
Dog dog = Dog.builder().species("哈士奇").action(dogAction).build();
Cat cat = Cat.builder().species("蓝猫").action(catAction).build();
Animal animal = Animal.builder().cat(cat).dog(dog).build();
//从对象里获取值
Optional.ofNullable(animal)
.map(Animal::getDog)
.map(Dog::getAction)
.ifPresent(var -> {
System.out.println("【Optional】" + var.getSleep());
});
//等同于
if (Objects.nonNull(animal)) {
Dog dogD = animal.getDog();
if (Objects.nonNull(dogD)) {
Action actionA = dogD.getAction();
if (Objects.nonNull(actionA)) {
System.out.println("【if】" + var.getSleep());
}
}
}
}
}
filter
Optional<T> filter(Predicate<? super T> predicate)
String str = "233";
Optional.ofNullable(str).filter(var -> var.length() == 3).ifPresent(var-> {
System.out.println("长度为3");
});
//输出:长度为3
//等同于:
if (null != str) {
if (str.length() == 3) {
System.out.println("长度为3");
}
}
flatMap
<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper)
public class TestMain {
public static void main(String[] args) {
Optional.ofNullable(animal)
.map(Animal::getDog)
.flatMap(TestMain::getDogSleep)
.ifPresent(var -> {
System.out.println(var);
});
private static Optional<String> getDogSleep(Dog dog) {
return Optional.ofNullable(dog).map(Dog::getAction).map(Action::getSleep);
}
}
}
...emmm这个我使用的不太多,因为他返回一个Optional<T>,所以可以像上边一样作代码抽取。