使用guva库解决NULL的问题

一个不好的示范:

  1. public List<User> listUser(){

  2. List<User> userList = userListRepostity.selectByExample(new UserExample());

  3. if(CollectionUtils.isEmpty(userList)){//spring util工具类

  4. return null;

  5. }

  6. return userList;

  7. }

    如果用JDK 8,可以使用optional,如果不是的话,可以使用google guva库:

     

  8. public List<User> listUser(){

  9. List<User> userList = userListRepostity.selectByExample(new UserExample());

  10. if(CollectionUtils.isEmpty(userList)){

  11. return Lists.newArrayList(); //guava类库提供的方式

  12. }

  13. return userList;

  14. }

发布了3527 篇原创文章 · 获赞 128 · 访问量 258万+

猜你喜欢

转载自blog.csdn.net/jackyrongvip/article/details/103592064