7

I have to choose between two String variables - the first that has non null value. If they are both null - then I want to exit the method. This can be done in the following piece of code:

String value1 = <get from somewhere>
String value2 = <get from somewhere else>
String target = null;
if (value1 != null) target = value1;
else if (value2 != null) target = value2;
if (target == null) return null;

It can also be done in short form:

String target = value1 != null ? value1 : value2 != null ? value2 : null;
if (target == null) return null;

I am struggling with how to do this in fluent form with Optional
Note: I can only use Java 8 syntax (so no Optional.or())

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47

3 Answers3

11
String target = Optional.ofNullable(value1).orElse(value2)

If value1 == null, then return value2 - which either has a value or is null. There is no need to explicitly handle the case of value2 == null by mapping it to null.

david a.
  • 5,283
  • 22
  • 24
8

If you have just two options, you could do it with:

target = Optional.ofNullable(value1)
            .orElseGet(() -> Optional.ofNullable(value2).orElse(null));

Even if this can be made to work for more that two variables.

With java-9, this could be made a bit more readable:

target = Optional.ofNullable(value1)
            .or(() -> Optional.ofNullable(value2))
            .orElse(null);

The other answer has a very good point, indeed; thus this can be further simplified to not use Optionals, at all, still a single line:

 target = value1 != null ? value1 : value2;
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • I think this is clearer than the `Stream.of` answer from another question. – Sharon Ben Asher Oct 11 '18 at 10:08
  • 1
    @SharonBenAsher not really... especially if you would have three variables, this will get ugly very fast. But it's an option, I guess – Eugene Oct 11 '18 at 10:08
  • yes. it is clear for two optionals. perhaps, for completeness, you can mention that Java 9 solved this with `Optional.or` – Sharon Ben Asher Oct 11 '18 at 10:09
  • plus the answer posted here is not posted in the other question – Sharon Ben Asher Oct 11 '18 at 10:11
  • @SharonBenAsher So he should post the answer on the other question. – Michael Oct 11 '18 at 10:12
  • @Eugene Fair enough. It's a still a clear duplicate that you shouldn't have re-opened. I don't want to get into a cycle of closing and re-opening it - so can you give a good reason why it's not a duplicate? – Michael Oct 11 '18 at 10:13
  • 2
    @Michael I have to re-read them when I have a bit more time and with a fresh mind, but this place is not one we should always agree on - you consider being a duplicate, please close it (again), untimely it will get deleted too, no offense whatsoever from me. thank you for the comments actually – Eugene Oct 11 '18 at 10:15
0

If you want to check AND condition

Optional.ofNullable(value)
                    .filter( res -> StringUtils.isNotEmpty((CharSequence) val.get()))
                    .ifPresent( finalResul -> {
                        //apply logic
                    });
naveen
  • 21
  • 3