Assign value of Optional to a variable if present

前端 未结 5 2422
栀梦
栀梦 2021-02-19 02:13

Hi I am using Java Optional. I saw that the Optional has a method ifPresent.

Instead of doing something like:

Optional object = someMeth         


        
5条回答
  •  旧时难觅i
    2021-02-19 02:45

    Quite late but I did following:

    String myValue = object.map(x->x.getValue()).orElse("");
                               //or null. Whatever you want to return.
    

提交回复
热议问题