将一个类对象列表映射到另一个列表

huangapple go评论113阅读模式
英文:

Mapping a list of class object to another list

问题

以下是翻译好的部分:

  1. 我有以下两个类一个是 `originalPerson`另一个是 `originalAddress``originalPerson` 包含一个名为 `location` 的可用地址列表
  2. class originalPerson (
  3. val firstName: kotlin.String,
  4. val LastName: kotlin.String,
  5. val isAvailable: kotlin.Boolean,
  6. val location: kotlin.collections.List<originalAddress>
  7. )
  8. class originalAddress (
  9. val first: String? = null,
  10. val second: String? = null,
  11. val third: String? = null,
  12. )
  13. 我还有两个类它们与上面的类非常相似但具有自定义数量的字段
  14. class customPerson (
  15. val firstName: kotlin.String,
  16. val LastName: kotlin.String,
  17. val location: kotlin.collections.List<customAddress>
  18. )
  19. class customAddress (
  20. val first: String? = null,
  21. val second: String? = null,
  22. )
  23. 我有一个方法它返回一个 `originalPerson` 对象但我想将其映射到 `customPerson` 对象并返回我不确定如何正确地做到这一点
  24. 我已经成功将 `originalPerson` `firstName` `lastName` 映射到 `customPerson`但我不知道如何将 `List<originalAddress>` 映射到 `List<customAddress>`以下是我目前的代码
  25. val result = it.value.flatMap { r -> r.fieldsMap.entries}
  26. .map { r -> customPerson(r.firstName, r.LastName, **List<customAddress>**)}
  27. 我会感激一些关于如何将 `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.

  1. class originalPerson (
  2. val firstName: kotlin.String,
  3. val LastName: kotlin.String,
  4. val isAvailable: kotlin.Boolean,
  5. val location: kotlin.collections.List<originalAddress>
  6. )
  7. class originalAddress (
  8. val first: String? = null,
  9. val second: String? = null,
  10. val third: String? = null,
  11. )

I have also two more class which are pretty similar to the classes above but with custom number of fields.

  1. class customPerson (
  2. val firstName: kotlin.String,
  3. val LastName: kotlin.String,
  4. val location: kotlin.collections.List<customAddress>
  5. )
  6. class customAddress (
  7. val first: String? = null,
  8. val second: String? = null,
  9. )

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:

  1. val result = it.value.flatMap { r -> r.fieldsMap.entries}
  2. .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):

  1. val result = it.value.flatMap { r -> r.fieldsMap.entries }
  2. .map { r -> customPerson(
  3. r.firstName,
  4. r.LastName,
  5. r.location.map { customAddress(it.first, it.second) }
  6. )
  7. }

我只是像你一开始做的那样使用了 map,并创建了一个自定义地址的列表。

如果你愿意,你可以为此创建一个方法:

  1. private fun toCustomAddresses(originalPerson: originalPerson) =
  2. originalPerson.location.map { customAddress(it.first, it.second) }

然后像这样使用它:

  1. val result = it.value.flatMap { r -> r.fieldsMap.entries }
  2. .map { r -> customPerson(
  3. r.firstName,
  4. r.LastName,
  5. toCustomAddresses(r)
  6. )
  7. }

甚至可以将其作为扩展函数:

  1. private fun List<originalAddress>.toCustomAddresses() =
  2. map { customAddress(it.first, it.second) }
  3. val result = it.value.flatMap { r -> r.fieldsMap.entries }
  4. .map { r -> customPerson(
  5. r.firstName,
  6. r.LastName,
  7. r.location.toCustomAddresses()
  8. )
  9. }

个人而言,我更喜欢这种方法,但我认为这取决于你和你自己的准则。

英文:

If I get it right you're almost there, just need another map:

  1. val result = it.value.flatMap { r -&gt; r.fieldsMap.entries}
  2. .map { r -&gt; customPerson(
  3. r.firstName,
  4. r.LastName,
  5. r.location.map { customAddress(it.first, it.second) }
  6. )
  7. }

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:

  1. private fun toCustomAddresses(originalPerson: originalPerson) =
  2. originalPerson.location.map { customAddress(it.first, it.second) }

And use it like:

  1. val result = it.value.flatMap { r -&gt; r.fieldsMap.entries}
  2. .map { r -&gt; customPerson(
  3. r.firstName,
  4. r.LastName,
  5. toCustomAddresses(r)
  6. )
  7. }

Or even as an extension function:

  1. private fun List&lt;originalAddress&gt;.toCustomAddresses() =
  2. map { customAddress(it.first, it.second) }
  3. val result = it.value.flatMap { r -&gt; r.fieldsMap.entries}
  4. .map { r -&gt; customPerson(
  5. r.firstName,
  6. r.LastName,
  7. r.location.toCustomAddresses()
  8. )
  9. }

Personally I prefer this approach, but I think it depends on you and your own guidelines.

huangapple
  • 本文由 发表于 2020年8月28日 17:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63631298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定