I am new to kotlin and I am trying to loop through two list and insert email in people.
data class User(val id:String,val name: String,val email: String)
data class Person(val id:String,val name: String, val email: String="")
//Input
val users = listOf(User("1","John","john@a.com"),User("2","Doe","Doe@a.com"))
val people = listOf(Person("1","John"),Person("2","Doe"))
//expected
val userToPerson = listOf(Person("1","John","john@a.com"),Person("2","Doe","Doe@a.com"))
I am trying with this.
val map = people.map { it ->
{
val foundUser = users.find { user -> user.id == it.id }
if (foundUser != null) {
it.email = foundUser.email
}
}
}
map.forEach(System.out::print)
I am getting error for foundUser.isNotNull() here Unresolved reference: isNotNull
Updated with suggested:
() -> kotlin.Unit() -> kotlin.Unit
I am trying to convert a list of users to a list of people. They both have their ids as common.
I want to update people Person class corresponding to their user email.
All people do not have email. But all users have the email.
So, the final result would have people with email. If there is a person with no matching id, we can skip that data.