I have a list, each element with the following fields: active: true/false, optional_id: Int?, name: String
I am filtering for fields that have active true, to get the highest optional_id.
testList.filter { it.active == true }
.maxByOrNull { it.optional_id }
?. optional_id ?: 0
The idea is, if there are objects in the list, with the field active true, to get the highest optional_id amongst those.
Else, if they are all false (no active field), then return 0.
But since optional_id is of type Int?, maxByOrNull does not accept it without asserting !!. Is there any other way to handle null assertions for this scenario ?
I can't change the type of optional_id.