Hi guys. First off, thanks for you replies. Happens that the real code doesn't have those strings. Instead it has respose = repository.[query]. So I'd like to find a way to avoid those nested if else-if statements. I think it could be done by using lambdas along with maps but all I was trying was unsuccessful.
Rob Spoor wrote:That second example is just evil...
I agree, I dislike it too.
I think that the original code is good enough. Since the else is implied, the condition is no longer needed. Here's a minor modification:
Having said that, there's a trick that you can do using Optional's map() method. The map method is evaluated only if an optional is present. This allows you to do something like this:In the above code,
if employee is null, we get a blank string
if employee is not null but address is null, we still get a blank string
if employee is not null and address is not null, but street is null, we still get a blank string
However, this does not match OP's requirement exactly and hence I would not suggest using Optional here.
Pablo, I assume you're going to use matrix and active in the query that you're sending to the repository. If my assumption is correct, can you please give us an example that matches your actual situation a bit more closely?
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.