Reputation: 2418
I have a list of type A
. I want to turn it into a list of type B
.
fun publish(listOne: List<A>) {
val result: List<B> =
}
Class B
looks like this. It has a constructor that if you pass A
then it will copy the variables and make a new instance of B
.
class B(name: String ....) {
constructor(a: A) {
// copies the values of a then creates an instance.
}
}
How can I use this constructor to make a new list of type B
? Normally I would traverse the entire list of and make a new instance of B
per instance of A
. However, is there a way to do this in Kotlin that is easy?
Upvotes: 1
Views: 435
Reputation: 3232
You can just map the given list:
val result: List<B> = listOne.map(::B)
// or
val result: List<B> = listOne.map { B(it) }
Upvotes: 3