Optional.ofNullable and method chaining

sebast26

I was surprised by Optional.ofNullable method. Some day I wrote a function that supposed to return an Optional:

private Optional<Integer> extractFirstValueFrom(InsightsResponse insight) {
    return Optional.ofNullable(insight.getValues().get(0).getValue());
}

I mistakenly thought that Optional.ofNullable will prevent any NullPointerExceptions inside of argument expression.

Now I think I know that it was very foolish idea. Java have to resolve arguments first to pass it to Optional.ofNullable invocation.

But I have a question. Is there a nice and good way to accomplish my goal? I would like to obtain from expression insight.getValues().get(0).getValue() some Integer value or null. Null can be each one of the expressions: insight.getValues() or insight.getValues().get(0).

I know that I can just put this in try/catch block but I am wondering if there is more elegant solution.

Tunaki

If you have no idea what can be null, or want to check everything for null, the only way is to chain calls to Optional.map:

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.

As such, if the mapper return null, an empty Optional will be returned, which allows to chain calls.

Optional.ofNullable(insight)
        .map(i -> i.getValues())
        .map(values -> values.get(0))
        .map(v -> v.getValue())
        .orElse(0);

The final call to orElse(0) allows to return the default value 0 if any mapper returned null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Optional.ofNullable with Mockito

From Dev

Optional chaining method on find with default value and a ternary operator

From Dev

How to port from java.util.Optional method call chaining to Guava Optional?

From Java

Java 8 Optional. Why of and ofNullable?

From Dev

Optional ofNullable execution flow in java 8

From Dev

Java 8 Optional. Why of and ofNullable?

From Dev

Should Optional.ofNullable() be used for null check?

From Dev

Null pointer exception inside Optional.ofNullable()

From Dev

Optional.ofNullable - Cannot return a void result

From Dev

Possible redundant object creation with Optional#ofNullable?

From Java

Optional chaining (?.) in nashorn

From Dev

Optional Chaining returning an Int

From Dev

Swift 3.0 Optional Chaining

From Dev

Optional Chaining in JavaScript

From Dev

swift optional chaining with cast

From Dev

Optional Chaining Not Working As Expected

From Dev

Optional chaining with Swift strings

From Dev

Chaining Optional.orElseThrow

From Dev

Optional chaining and Array in swift

From Dev

Optional chaining for constructor calls?

From Java

Why use Optional.of over Optional.ofNullable?

From Dev

How does this work - Java 8 Optional.of and Optional.ofNullable

From Dev

whats the difference between Optional.ofNullable vs Optional.fromNullable

From Dev

Method chaining with the same method

From Dev

Optional chaining not working for optional protocol requirements

From Dev

Is there an equivalent to optional chaining with arithmetic operators?

From Dev

dynamicType of optional chaining not the same as assignment

From Dev

Optional Chaining or ternary expression in Swift?

From Java

Is using Optional.ofNullable as a replacement for the ternary operator a good practice?

Related Related

  1. 1

    Java Optional.ofNullable with Mockito

  2. 2

    Optional chaining method on find with default value and a ternary operator

  3. 3

    How to port from java.util.Optional method call chaining to Guava Optional?

  4. 4

    Java 8 Optional. Why of and ofNullable?

  5. 5

    Optional ofNullable execution flow in java 8

  6. 6

    Java 8 Optional. Why of and ofNullable?

  7. 7

    Should Optional.ofNullable() be used for null check?

  8. 8

    Null pointer exception inside Optional.ofNullable()

  9. 9

    Optional.ofNullable - Cannot return a void result

  10. 10

    Possible redundant object creation with Optional#ofNullable?

  11. 11

    Optional chaining (?.) in nashorn

  12. 12

    Optional Chaining returning an Int

  13. 13

    Swift 3.0 Optional Chaining

  14. 14

    Optional Chaining in JavaScript

  15. 15

    swift optional chaining with cast

  16. 16

    Optional Chaining Not Working As Expected

  17. 17

    Optional chaining with Swift strings

  18. 18

    Chaining Optional.orElseThrow

  19. 19

    Optional chaining and Array in swift

  20. 20

    Optional chaining for constructor calls?

  21. 21

    Why use Optional.of over Optional.ofNullable?

  22. 22

    How does this work - Java 8 Optional.of and Optional.ofNullable

  23. 23

    whats the difference between Optional.ofNullable vs Optional.fromNullable

  24. 24

    Method chaining with the same method

  25. 25

    Optional chaining not working for optional protocol requirements

  26. 26

    Is there an equivalent to optional chaining with arithmetic operators?

  27. 27

    dynamicType of optional chaining not the same as assignment

  28. 28

    Optional Chaining or ternary expression in Swift?

  29. 29

    Is using Optional.ofNullable as a replacement for the ternary operator a good practice?

HotTag

Archive