The main issue is that Scala does not support the ternary operator you described. That is supported Java but there's no need for it in Scala.
In Java the main difference between the if
statement and the ternary operator is that the latter is an expression, meaning that it's evaluated to a result, while if
is (as I previously suggested) a statement that relies on side-effects within its scope to make things happen.
In Scala if
is already an expression, hence there is no need for a ternary operator.
Your code would be structured as follows:
val filteredList = list.filter { l => if (pid == "") true else l.ProviderId.toUpperCase().contains(pid.toUpperCase()) }
As suggested in a comment you can further improve the readability by not relying on if
s to express simple boolean conditions.
val filteredList = list.filter { l => pid == "" || l.ProviderId.toUpperCase().contains(pid.toUpperCase())) }
Furthermore, in your case, pid
seems to be external to the list itself, so maybe pulling it out of the filter
(which has O(n) complexity on List
) can save you some cycles:
val filteredList = if (pid.isEmpty) list else list.filter(_.ProviderId.toUpperCase().contains(pid.toUpperCase()))
It also looks like you are trying to make a case-insensive equality check on the two string, in which case you may be interested in using a Pattern
and not converting pid
to upper case at every loop:
val pidPattern = Pattern.compile(Pattern.quote(pid), Pattern.CASE_INSENSITIVE)
val filteredList = if (pid.isEmpty) list else list.filter(l => pidPattern.matcher(l.ProviderId).find)