The closest thing I could find is zip, which does pretty much what I want, but it uses the index. I want to specify a field on which the two lists get joined if they are the same value.
In SQL one would use "table1 INNER JOIN table2 WHERE table1.field = table2.field". Is there something similar in Kotlin?
Maybe this example makes it clearer:
class Something(val id : Int, val value : Int)
val list1 = listOf(Something(0, 1), Something(1, 6), Something(2, 8))
val list2 = listOf(Something(1, 2), Something(5, 3), Something(9, 6))
val result = list1.innerJoin(list2, on=id).map { element1, element2 -> element1.value * element2.value}
//should return [12] (6*2)
list1 and list2 both have an element with an id=1, so their values (6 and 2) get multiplied in this example and the result should be 12.
Currently I use this code snippet, which works, but I was wondering if there is an easier and more efficient way to do this.
val result = list1.map { element1 ->
val element2 = list2.find { element2 -> element2.id == element1.id } ?: return@map null
element1.value * element2.value
}.filterNotNull()
Thanks.