java中 .isPresent()方法的使用

在java中,为了判断查询的类对象是否存在,采用此方法:

eg:

public ShopHomeView findByOwnerIds(String ownerId) {

Optional<ShopHomeView> view = shopHomeViewRepository.findByOwnerId(ownerId);
if(view.isPresent()) {
return view.get();
}else {
return null;
}
}

.isPresent()一般与.get()方法合用,当view存在时,view.isPresent()值为true,通过get()方法返回对象。

猜你喜欢

转载自www.cnblogs.com/qqzhulu/p/10176757.html