如何通过id合并两个对象列表并选择非空值在Kotlin中

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

How to combine two object lists by id and select non-null values in kotlin

问题

  1. 我想要合并两个对象列表。每个对象都有一个特定的id。如果同一个对象存在于两个列表中,我想要将它们合并。而且如果一个值为空,我想要获取非空的值。例如:
  2. val list1 = listOf(
  3. Object(id = 1, hello, 100),
  4. Object(id = 2, null, 40)
  5. )
  6. val list2 = listOf(
  7. Object(id = 1, null, 100),
  8. Object(id = 2, test, 40),
  9. Object(id = 3, hi, 13)
  10. )
  11. 我想要实现的结果如下:
  12. val result = listOf(
  13. Object(id = 1, hello, 100),
  14. Object(id = 2, test, 40),
  15. Object(id = 3, hi, 13)
  16. )
  17. **注意:如果一个值不为空,我知道它是相同的。**
英文:

I want to combine two object lists. Each object has a specific id. If the same object exists in two lists, I want to merge them. And if a value is null I want to get the non-null one. For example:

  1. val list1 = listOf(
  2. Object(id = 1, hello, 100),
  3. Object(id = 2, null, 40)
  4. )
  5. val list2 = listOf(
  6. Object(id = 1, null, 100),
  7. Object(id = 2, test, 40),
  8. Object(id = 3, hi, 13)
  9. )

The result I want to achieve is as follows:

  1. val result = listOf(
  2. Object(id = 1, hello, 100),
  3. Object(id = 2, test, 40),
  4. Object(id = 3, hi, 13)
  5. )

NOTE: if a value is not empty I know it is not different.

答案1

得分: 3

  1. fun main() {
  2. val list1 = listOf(
  3. Obj(id = 1, "你好", 100),
  4. Obj(id = 2, null, 40)
  5. )
  6. val list2 = listOf(
  7. Obj(id = 1, null, 100),
  8. Obj(id = 2, "测试", 40),
  9. Obj(id = 3, "嗨", 13)
  10. )
  11. val result = (list1.asSequence() + list2)
  12. .groupingBy { it.id }
  13. .reduce { _, accumulator: Obj, element: Obj ->
  14. accumulator.copy(second = accumulator.second ?: element.second)
  15. }
  16. .values.toList()
  17. println(result)
  18. }
  19. data class Obj(val id: Int, val second: String?, val third: Int)
英文:
  1. fun main() {
  2. val list1 = listOf(
  3. Obj(id = 1, "hello", 100),
  4. Obj(id = 2, null, 40)
  5. )
  6. val list2 = listOf(
  7. Obj(id = 1, null, 100),
  8. Obj(id = 2, "test", 40),
  9. Obj(id = 3, "hi", 13)
  10. )
  11. val result = (list1.asSequence() + list2)
  12. .groupingBy { it.id }
  13. .reduce { _, accumulator: Obj, element: Obj ->
  14. accumulator.copy(second = accumulator.second ?: element.second)
  15. }
  16. .values.toList()
  17. println(result)
  18. }
  19. data class Obj(val id: Int, val second: String?, val third: Int)

答案2

得分: 0

有两种方法可以解决这个问题,一种是O(n^2)的方法,其中你要遍历两个列表并检查每个对象。另一种是O(n)的方法,其中你会浪费一些额外的空间,通过在一个字典中创建映射,然后在一个列表上进行循环,同时在另一个列表上进行检查。

英文:

There can be 2 approaches to the problem, O(n^2) approach where you loop through both the lists and check for each object. Or O(n) approach where you waste some extra space by creating a mapping in one dictionary for your list and looping on one while checking in other.

huangapple
  • 本文由 发表于 2020年10月7日 03:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64232688.html
匿名

发表评论

匿名网友

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

确定