I would like to find an element in a list that matches a predicate and to get a default value if no elements matches the predicate. I want to do this idiomatically, without defining additional variables (one liner).
Is there something similar to getOrElse of HashMap?
I would like to find an element in a list that matches a predicate
Share
This is yet another example of why
Optionis great!The
findmethod returns anOption, andOptionhas a methodgetOrElsethat does exactly what you want.When nothing is found,
findreturnsNone, which means the “else” value will be returned.