something().orElseGet(() -> (a, b, c) -> {})
// ^----this part------^
where (a, b, c)
is a method with a
, b
and c
parameters. e.g: Method(a, b, c)
which returns something.
My question is in practice what does this functional interface part, for me it's confusing.
It returns a Supplier
that then supplies the three parameter method implementation (a, b, c) -> {}
Usually, all method parameters in Java are evaluated as soon as a method is called.
This means, that in the hypothetical case of orElseGet((a, b, c) -> {})
the method would always be created, whether it is needed or not. But as it is only needed when there is no other value present, having the intermediate supplier means that the method is only created when it is actually needed.
As the value that is used in the case that there is no value present might be arbitrarily costly to create, the supplier is potentially a huge time saver. In your case there will not be much of a difference (creating a supplier or creating a lambda is probably about equal in terms of cost). But there can be other situations where getting the value might involve database lookups, string concatenation etc, where it can be a huge benefit to only perform those actions when they are actually needed.