Reputation: 6236
In my service I want to return the MAX+1 OR return 10000000 instead, I have fooRepository.findMax()
which return an Optional<Long>
which is the MAX.
How to do it in just one line like below (I miss just the increment part)
fooRepository.findMax().orElse(10000000L)
PS: I know I can do it in multiple lines with ifPresent
...
Upvotes: 0
Views: 95
Reputation: 1095
You can use the map method to do something with the contents of the Optional. If the Optional is empty, it is not executed.
Like this:
fooRepository.findMax()
.map(max -> max + 1)
.orElse(10000000L);
Upvotes: 3
Reputation: 2890
That's what map
is for:
fooRepository.findMax().map(m -> m + 1).orElse(10000000L)
Upvotes: 3
Reputation: 2155
This seems like a weird requirement, but why not just do...
fooRepository.findMax().orElse(9999999L) + 1
?
Upvotes: 3