【发布时间】:2021-01-21 17:45:17
【问题描述】:
我想合并两个对象列表。每个对象都有一个特定的 ID。如果两个列表中存在同一个对象,我想合并它们。如果一个值为空,我想获得非空值。例如:
val list1 = listOf(
Object(id = 1, hello, 100),
Object(id = 2, null, 40)
)
val list2 = listOf(
Object(id = 1, null, 100),
Object(id = 2, test, 40),
Object(id = 3, hi, 13)
)
我想要达到的结果如下:
val result = listOf(
Object(id = 1, hello, 100),
Object(id = 2, test, 40),
Object(id = 3, hi, 13)
)
注意:如果一个值不为空,我知道它没有不同。
【问题讨论】:
-
列表中是否只有唯一值或者可以重复?例如
val list1 = listOf(Object(id = 1, null, 100), Object(id = 1, hello, 100)) -
只有唯一值
-
您的代码无法编译。请提供Minimal, Reproducible Example
标签: java android list kotlin merge