我有两个对象列表,它们都实现了一个接口,但在其他方面是不相关的。如何创建一个新的对象集合,其中只包含与其他列表中的值匹配的其中一个列表的对象?
显然我可以使用for循环和放大器手动执行此操作,但我想知道如何使用Kotlin的标准库集合过滤功能来完成此操作。
所以这是一个例子:
interface Ids
{
val id: Int
}
data class A(override val id: Int, val name: String) : Ids
data class B(override val id: Int, val timestamp: Long) : Ids
fun main(args: Array<String>) {
val a1 = A(1, "Steve")
val a2 = A(2, "Ed")
val aCol = listOf(a1, a2)
val b2 = B(2, 12345)
val b3 = B(3, 67890)
val bCol = listOf(b2, b3)
val matches = mutableListOf<B>()
// This is where I'm stuck.
// I want to filter bCol using objects from aCol as a filter.
// The result should be that matches contains only a single object: b2
// because bCol[0].id == aCol[1].id
// I'm guessing I need to start with something like this:
bCol.filterTo(matches) { ??? }
}
答案 0 :(得分:5)
一种直截了当的方法是在aCol
中搜索b
bCol
中每个bCol.filter { b -> aCol.any { a -> a.id == b.id } }
具有相同ID的对象:
aCol
但是如果你的名单足够大,那可能会变得太慢。
为了使其更具可扩展性,您可以先在val aColIds = aCol.map { it.id }.toSet()
中构建一组所有ID:
Set.contains
然后使用b.id
方法确定aColIds
中是否bCol.filter { it.id in aColIds }
// or equivalent
bCol.filter { aColIds.contains(it.id) }
:
{{1}}