简体   繁体   中英

What am i doing wrong in this Java optional - orElse statement

 List<String> list = 
  Arrays.asList("TEST1", "TEST2", "TEST3");

    final String finalStr = 
Optional.ofNullable(list.stream()
            .filter(s -> s.contains("pattern:a:b:"))
            .map(str -> str.substring(str.lastIndexOf(':') + 1))
            .findFirst())
            .orElse(list.stream()
                    .filter(s -> !s.contains("pattern:c:d:"))
                    .findFirst())
            .orElseThrow();

It will work if the list contains pattern:a:b: but for above example always throw an exception if that pattern is not available. In the orElse part it doesnt even seem to be executing.

My result is [java] => java.util.NoSuchElementException: No value present when i am epecting to return me the first element.

Edit: I wish to first find if any string with pattern:a:b exists in the list, if it does not, i wish it to return me the first string that does not contain the pattern:c:d For example

"pattern:a:b:TEST1", "pattern:c:d:TEST2", "TEST3" should always return TEST1.

"pattern:c:d:TEST1", "TEST2", "TEST3" should always return TEST2.

"TEST1", "TEST2", "TEST3" should always return TEST1.

Let's follow the evaluation. Since the inner part of the Optional.ofNullable(...) results in Optional<String> , the entire chain wraps it in Optional<Optional<String>> .

After evaluation of the parameter in Optional.ofNullable(...) , the result is roughly this:

Optional.ofNullable(Optional.<String>empty())      // Optional<Optional<String>>
                .orElse(list.stream().findFirst()) // Optional<String>
                .orElseThrow();                    // String

The orElse is not called as long as the Optional.empty() is not null , hence the outer Optional is not empty . The flow continues with the inner Optional.enpty on which the orElseThrow is called and always fails by throwing java.util.NoSuchElementException .

What you want to do is this:

final String finalStr = list.stream()                        // Stream<String>
        .filter(s -> s.contains("pattern:a:b:"))             // Stream<String>
        .map(str -> str.substring(str.lastIndexOf(':') + 1)) // Stream<String>
        .findFirst()                                         // Optional<String>
        .or(() -> list.stream().findFirst())                 // Optional<String>
        .orElseThrow();                                      // String

The or method is the key one as on empty Optional<T> it supplies with a different one without losing the Optional<T> context.

Your misunderstanding is that findFirst returns null if the stream is empty, and tries to use Optional.ofNullable to wrap it into an Optional . findFirst in fact returns an Optional<T> directly, so you end up with a double optional - Optional<Optional<String>> . And the outer optional is never empty, since findFirst always returns a non-null value (that could be either an empty or a non-empty Optional ).

Then, you use orElse on it. Since the outer optional is never empty, this always unwraps the outer optional and the default value you provide is never used. Finally, orElseThrow is called on the inner optional, which could be empty or non-empty, as that is what is returned by the first findFirst . If it happens to be empty, you'd get the exception.

You should not wrap the first findFirst in Optional.ofNullable in the first place. Use or to supply another Optional as the alternative when the optional is empty.

final String finalStr =
    list.stream()
            .filter(s -> s.contains("pattern:a:b:"))
            .map(str -> str.substring(str.lastIndexOf(':') + 1))
            .findFirst()
        .or(() ->
            list.stream()
                .filter(s -> !s.contains("pattern:c:d"))
                .findFirst()
        )
        .orElseThrow();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM