Optional Getting Field

你说的曾经没有我的故事 提交于 2019-11-28 12:21:51

What you are describing is the map method:

Integer valA = foo.getFoob().map(f -> f.getValA()).orElse(null);

map lets you transform the value inside an Optional with a function if the value is present, and only changes the type of the optional if the value in not present.

Note also that you can return null from the mapping function, in which case the result will be Optional.empty().

Why you dont add a getValue methode to the class Foo? This would be a kind of delegation.

public class Foo {
   ...
   public Integer getValue() {
       if (foob == null) {
          return null;
       }
       return foob.getValA();
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!