【Optional】告别丑陋判空,使用Optional类
一、概述
当项目中充斥着大量的、丑陋的判空语句,如下:
if (user != null) {Address address = user.getAddress();if (address != null) {Country country = address.getCountry();if (country != null) {String isocode = country.getIsocode();if (isocode != null) {isocode = isocode.toUpperCase();}}}
}
那怎么办呢?使用Optional优化后:
String result = Optional.ofNullable(user).map(u -> u.getAddress()).map(a -> a.getCountry()).map(c -> c.getIsocode()).orElse("default");
二、创建Optional实例
Optional类, 是对value值进行了包装,它的值可能是null, 也可能不是null,一共有两个方法创建Optional实例
2.1 static Optional of(T value)
@Test
public void testOf() {Integer value = 20;// 正常执行Optional<Integer> op = Optional.of(value);value = null;// 报空指针op = Optional.of(value);
}
2.2 static Optional ofNullable(T value)
@Testpublic void testOfNullable() {Integer value = 2;// 正常Optional<Integer> op = Optional.ofNullable(value);value = null;// 也正常 不报错op = Optional.ofNullable(value);}
2.3源码分析
empty()的作用就是返回EMPTY对象。
相比较of(T value)
的区别就是,当value值为null时,of(T value)
会报NullPointerException异常;ofNullable(T value)
不会throw Exception,ofNullable(T value)
直接返回一个EMPTY对象。
三、获取Optional中的值
3.1T get()
获取原生value的方法,如果value是空,则直接抛出异常,否则返回。
@Test
public void testGet() {Integer value = 2;// 正常Optional<Integer> op = Optional.ofNullable(value);Integer opVal = op.get();Assert.assertEquals(opVal, value);op = Optional.ofNullable(null);// 抛出异常op.get();
}
源码:
3.2T orElse(T other)
如果值存在返回,否则返回orElse中传入的other
@Test
public void testOrElse() {// 正常Optional<Integer> op = Optional.ofNullable(2);Integer opVal = op.orElse(3);Assert.assertEquals(opVal, new Integer(2));op = Optional.ofNullable(null);// 为空,则返回3opVal = op.orElse(3);Assert.assertEquals(opVal, new Integer(3));
}
源码:
3.3T orElseGet(Supplier<? extends T> other)
如果存在则返回该值,否则调用other这个函数编程并返回该调用的结果。
@Test
public void testOrElseGet() {// 正常Optional<Integer> op = Optional.ofNullable(2);Integer opVal = op.orElseGet(()-> {return new Integer(3);});Assert.assertEquals(opVal, new Integer(2));op = Optional.ofNullable(null);// 如果为空,则返回3opVal = op.orElseGet(()-> {return new Integer(3);});Assert.assertEquals(opVal, new Integer(3));
}
源码:
3.4orElseGet和orElse有什么区别吗?
orElse()
和 orElseGet()
的不同之处在于当 ofNullable()
传入参数不为空时,orElse()
方法仍然创建了 other这个 对象。与之相反,orElseGet()
方法不创建对象。在执行较密集的调用时,比如调用 Web 服务或数据查询,这个差异会对性能产生重大影响。
而且我们还可以在orElseGet
方法中加些日志,可以把这种为空的异常情况暴露出来。
3.5T orElseThrow(Supplier<? extends X> exceptionSupplier)
如果存在则返回该值,否则为空的话可以抛出自定义的异常
@Test
public void testOrElseThrow() {Optional<Integer> op = Optional.ofNullable(null);// 为空,则抛出指定的异常类型Integer opVal = op.orElseThrow(()-> {return new RuntimeException();});// 或抛出runtime异常Assert.assertEquals(opVal, new Integer(3));
}
源码:
四、判断Optional是否为空
4.1boolean isPresent()
判断value是否为空
@Test
public void testIsPresent() {Optional<Integer> op = Optional.ofNullable(null);// 为空Assert.assertFalse(op.isPresent());
}
4.2void ifPresent(Consumer<? super T> consumer)
如果存在值,则调用对应的consumer方法,否则不执行任何操作。
@Test
public void testIfPresent() {Optional<Integer> op = Optional.ofNullable(5);op.ifPresent(value -> {// 如果存在,打印出来System.out.println(value);});
}
五、Optional中的过滤、转换方法
5.1Optional filter(Predicate<? super T> predicate)
对Optional中的value进行过滤,如果不匹配,返回空
@Test
public void testFilter() {// 不满足过滤条件,返回空 OptionalOptional<String> op = Optional.ofNullable("10").filter(item -> "15".equals(item));Assert.assertFalse(op.isPresent());
}
5.2Optional map(Function<? super T, ? extends U> mapper)
对Optional中的value进行转换映射为另外一个对象,如果value为空,返回empty Optional
@Testpublic void testMap(){Optional<Optional<Integer>> integer = Optional.ofNullable("5").map(item -> Optional.of(Integer.valueOf(item)));}
源码:
5.3Optional flatMap(Function<? super T, Optional> mapper)
接受一个返回值为Optional的映射函数参数,该返回值亦是flatMap方法的返回值若结果为空,则返回 空Optional。它也map的区别,我们用一个例子演示出来。
@Testpublic void testflatMap(){Optional<Integer> integer = Optional.ofNullable("5").flatMap(item -> Optional.of(Integer.valueOf(item)));}
源码:
小结: 如果对于返回值非Optional类型,可以用map方法, 否则使用flatMap更加方便
六、实战案例
6.1例一
以前写法:
public String getCity(User user) throws Exception{if(user!=null){if(user.getAddress()!=null){Address address = user.getAddress();if(address.getCity()!=null){return address.getCity();}}}throw new Excpetion("取值错误");}
JAVA8写法:
public String getCity(User user) throws Exception{return Optional.ofNullable(user).map(u-> u.getAddress()).map(a->a.getCity()).orElseThrow(()->new Exception("取指错误"));
}
6.2例二
以前写法:
if(user!=null){dosomething(user);
}
JAVA8写法:
Optional.ofNullable(user).ifPresent(u->{dosomething(u);
});
6.3例三
以前写法:
public User getUser(User user) throws Exception{if(user!=null){String name = user.getName();if("zhangsan".equals(name)){return user;}}else{user = new User();user.setName("zhangsan");return user;}
}
JAVA8写法:
public User getUser(User user) {return Optional.ofNullable(user).filter(u->"zhangsan".equals(u.getName())).orElseGet(()-> {User user1 = new User();user1.setName("zhangsan");return user1;});
}