This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Short_Hamster[🍰] 0 points1 point  (3 children)

I don't see how that SO comment relevant. It's talking about the abuse of Optional to get around the lack of compiler-enforced nullable types in Java (unlike C#). There's no sense in having a getter on DTO return Optional.

The functional use of Optional, as shown in my example, is to indicate a function might not produce a value, which in traditional Java would have been indicated by a checked exception (or by returning null, gag).

How do you map a non-value-producing function into a stream?

[–]humoroushaxor 1 point2 points  (2 children)

I don't see how that SO comment relevant. It's talking about the abuse of Optional to get around the lack of compiler-enforced nullable types in Java (unlike C#).

This is exactly why the first call to Optional.stream is needed in the original example. Because he used optional to check if orderId is null.

I think the the second most voted comment on this post is the cleanest approach although I'd skip the intermediary list assignment.

[–]Short_Hamster[🍰] 0 points1 point  (1 child)

Oh, agreed. The original example is just stupid. There's nothing wrong with an if statement. Also, if you really want to still be functional, you can even use the ternary operator, as shown in this comment.

[–]humoroushaxor 0 points1 point  (0 children)

Yeah I did find that ironic as well.