I have a list of custom objects. I would like to retrieve any 2 that meet 2 different conditions.
E.g.
Assume
class Person {
val age: Int
//etc
}
And I have a List<Person>. I would like to get 2 Person that have age == 21 or age == 31
I know I can do a loop for that and break using some flag once I find both, but I was wondering if there was some more Kotlin idiomatic way for this instead of:
var p1: Person?
var p2: Person?
list.forEach{
if(it.age == 21) {
p1 = it
}
else if(it.age == 31) {
p2 = it
}
if(p1 != null && p2 != null) break
}