我对代码进行了一些简化,得到了以下内容:
fun intersection(data: List<HashSet<Protein>>) =
data.reduce { acc, it -> acc.apply { retainAll(it) } }
fun intersection(data: List<HashSet<Protein>>, combination: List<Int>) =
intersection(combination.map { data[it - 1] })
我通过以下方式获得了预期的结果:
val s1 = hashSetOf(Protein("1",2.0, 2.0,1), Protein("2",2.0, 2.0,1))
val s2 = hashSetOf(Protein("3",2.0, 2.0,1), Protein("2",2.0, 2.0,1))
val s3 = hashSetOf(Protein("3",2.0, 2.0,1), Protein("4",2.0, 2.0,1))
println(intersection(listOf(s1,s2,s3), listOf(1,2))) //[Protein(id=2, score=2.0, molw=2.0, spc=1)]
因此没有发生错误。你能提供你的测试代码吗?