英文:
Mapping a list of class object to another list
问题
以下是翻译好的部分:
我有以下两个类。一个是 `originalPerson`,另一个是 `originalAddress`。`originalPerson` 包含一个名为 `location` 的可用地址列表。
class originalPerson (
val firstName: kotlin.String,
val LastName: kotlin.String,
val isAvailable: kotlin.Boolean,
val location: kotlin.collections.List<originalAddress>
)
class originalAddress (
val first: String? = null,
val second: String? = null,
val third: String? = null,
)
我还有两个类,它们与上面的类非常相似,但具有自定义数量的字段。
class customPerson (
val firstName: kotlin.String,
val LastName: kotlin.String,
val location: kotlin.collections.List<customAddress>
)
class customAddress (
val first: String? = null,
val second: String? = null,
)
我有一个方法,它返回一个 `originalPerson` 对象,但我想将其映射到 `customPerson` 对象并返回。我不确定如何正确地做到这一点。
我已经成功将 `originalPerson` 的 `firstName` 和 `lastName` 映射到 `customPerson`,但我不知道如何将 `List<originalAddress>` 映射到 `List<customAddress>`。以下是我目前的代码:
val result = it.value.flatMap { r -> r.fieldsMap.entries}
.map { r -> customPerson(r.firstName, r.LastName, **List<customAddress>**)}
我会感激一些关于如何将 `List<originalAddress>` 映射到 `List<customAddress>` 的语法帮助。提前感谢。
英文:
I have the following two classes. An originalPerson
and an originalAddress
. An originalPerson
has a list of available addresses named as location
.
class originalPerson (
val firstName: kotlin.String,
val LastName: kotlin.String,
val isAvailable: kotlin.Boolean,
val location: kotlin.collections.List<originalAddress>
)
class originalAddress (
val first: String? = null,
val second: String? = null,
val third: String? = null,
)
I have also two more class which are pretty similar to the classes above but with custom number of fields.
class customPerson (
val firstName: kotlin.String,
val LastName: kotlin.String,
val location: kotlin.collections.List<customAddress>
)
class customAddress (
val first: String? = null,
val second: String? = null,
)
I have a method that returns an originalPerson
object but I want to map it to customPerson
object and return that instead. I am not sure how to correctly do that.
I have come as far as mapping firstName
and lastName
of originalPerson
to customPerson
but I don´t know how to map List<originalAddress>
to List<customAddress>
. Here´s my code so far:
val result = it.value.flatMap { r -> r.fieldsMap.entries}
.map { r -> customPerson(r.firstName, r.LastName, **List<customAddress>**)}
I would appreciate some help with the syntax of mapping List<originalAddress>
to List<customAddress>
. Thanks in advance.
答案1
得分: 2
如果我理解正确,你已经接近成功,只需要另一个映射(map):
val result = it.value.flatMap { r -> r.fieldsMap.entries }
.map { r -> customPerson(
r.firstName,
r.LastName,
r.location.map { customAddress(it.first, it.second) }
)
}
我只是像你一开始做的那样使用了 map
,并创建了一个自定义地址的列表。
如果你愿意,你可以为此创建一个方法:
private fun toCustomAddresses(originalPerson: originalPerson) =
originalPerson.location.map { customAddress(it.first, it.second) }
然后像这样使用它:
val result = it.value.flatMap { r -> r.fieldsMap.entries }
.map { r -> customPerson(
r.firstName,
r.LastName,
toCustomAddresses(r)
)
}
甚至可以将其作为扩展函数:
private fun List<originalAddress>.toCustomAddresses() =
map { customAddress(it.first, it.second) }
val result = it.value.flatMap { r -> r.fieldsMap.entries }
.map { r -> customPerson(
r.firstName,
r.LastName,
r.location.toCustomAddresses()
)
}
个人而言,我更喜欢这种方法,但我认为这取决于你和你自己的准则。
英文:
If I get it right you're almost there, just need another map:
val result = it.value.flatMap { r -> r.fieldsMap.entries}
.map { r -> customPerson(
r.firstName,
r.LastName,
r.location.map { customAddress(it.first, it.second) }
)
}
I just use map
like you did in the first place and create a list of custom addresses.
You can, if you want, create a method for this:
private fun toCustomAddresses(originalPerson: originalPerson) =
originalPerson.location.map { customAddress(it.first, it.second) }
And use it like:
val result = it.value.flatMap { r -> r.fieldsMap.entries}
.map { r -> customPerson(
r.firstName,
r.LastName,
toCustomAddresses(r)
)
}
Or even as an extension function:
private fun List<originalAddress>.toCustomAddresses() =
map { customAddress(it.first, it.second) }
val result = it.value.flatMap { r -> r.fieldsMap.entries}
.map { r -> customPerson(
r.firstName,
r.LastName,
r.location.toCustomAddresses()
)
}
Personally I prefer this approach, but I think it depends on you and your own guidelines.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论