Java 8 之 Optional
背景介绍:在我们实际开发中,经常会遇到各种 NullPointerException 报错,为了避免此类问题,需要在业务代码上添加各种if判断,使代码看起来很臃肿,而且很不美观
使用Optional 提供的各种方法,能够很好的解决上述的问题,下面介绍一下Optional的基础API
准备工作:新建一个User类及Address类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String id;
private String name;
private Address address;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Address {
private String province;
private String city;
}
1.orElse
当value为null时,取value1,否则取value
User user = null;
// 1.user为null时
User user1 = new User("001", "user1", null);
User result = Optional.ofNullable(user).orElse(user1);
System.out.println(result); // 输出 User(id=001, name=user1, address=null)
// 2.user 不为null时
user = new User("000","user",null);
result = Optional.ofNullable(user).orElse(user1);
System.out.println(result); //输出 User(id=000, name=user, address=null)
2.orElseThrow
当value为null时,抛出参数中指定的异常
User user = null;
// 写法1
User result = Optional.ofNullable(user).orElseThrow(NullPointerException::new);
// 写法2
User result = Optional.ofNullable(user).orElseThrow(()->new Exception("user为空"));
3.orElseGet
如果存在则返回该值,否则调用other值并返回该调用的结果。
User user = null;
User result = Optional.ofNullable(user).orElseGet(()->new User("1111","other",null));
System.out.println(result); // 输出 User(id=1111, name=other, address=null)
4.isPresent
如果存在值,则返回true ,否则返回false
User user = null;
boolean present = Optional.ofNullable(user).isPresent();
System.out.println(present); // false
User user1 = new User();
boolean present1 = Optional.ofNullable(user1).isPresent();
System.out.println(present1); // true
5.ifPresent
如果 存在值,则执行定义代码,否则什么也不做。
User user = null;
Optional<User> user1 = Optional.ofNullable(user);
user1.ifPresent(u -> System.out.println(u)); // 什么都不输出
User user = new User();
Optional<User> user1 = Optional.ofNullable(user);
user1.ifPresent(u -> System.out.println(u)); // User(id=null, name=null, address=null)
6.map
如果存在值,则对其应用提供的映射函数,如果结果为非 null,则返回描述结果的Optional 。否则返回一个空的Optional
Address address = new Address("浙江","杭州");
User user = new User();
user.setId("001");
user.setName("ll");
user.setAddress(address);
String s = Optional.ofNullable(user)
.map(u -> u.getAddress())
.map(address1 -> address1.getCity())
.orElseThrow(() -> new Exception("111"));
System.out.println(s); // 杭州
7.flatMap
如果存在值,则将提供的Optional函数应用于它,返回该结果,否则返回空Optional 。与map的不同是 flatMap提供的函数必需是Optional的
Address address = new Address("浙江","杭州");
User user = new User();
user.setId("001");
user.setName("ll");
user.setAddress(address)
Optional<String> s1 = Optional.ofNullable(user)
.flatMap(user1 -> Optional.of(user1.getAddress()))
.flatMap(address1 -> Optional.of(address1.getCity()));
System.out.println(s1.orElse("null")); // 杭州
8.filter
对 Optional 中包含的值进行过滤,如果包含的值满足条件,那么还是返回这个 Optional;否则返回 Optional.empty。
Address address = new Address("浙江","杭州");
User user = new User();
user.setId("001");
user.setName("ll");
user.setAddress(address);
String a = Optional.ofNullable(user)
.filter(user1 -> user1.getId().equals("001"))
.map(user1 -> user1.getId())
.orElse("null");
System.out.println(a); // 001
String b = Optional.ofNullable(user)
.filter(user1 -> user1.getId().equals("002"))
.map(user1 -> user1.getId())
.orElse("null");
System.out.println(b); // null