vg2--java8的使用和一些guava的使用包括options判断对象与集合空

这里主要讲下 对象判断空 和集合判断空用java8来实现

  

判断集合为空 更优雅的方式

Optional<List> optional = Optional.ofNullable(luUserService.selectAll());
result.setResult(optional.map(List -> luUserService.selectAll()).orElse(Collections.emptyList()));
result.setSuccess(true);

判断对象为空 更优雅方式

Optional<LuUser> optionalLuUserId3 = Optional.ofNullable(luUserService.selectId3());
optionalLuUserId3.ifPresent(item->{
result.setResult(item);
result.setSuccess(true);
});

猜你喜欢

转载自www.cnblogs.com/jiaowoxiaofeng/p/12117111.html