我想知道如何使用循环连接多个列表。这是我正在尝试做的一个例子:
object MyObj {
var objs = Set (
MyObj("MyObj1", anotherObjList),
MyObj("MyObj2", anotherObjList)
)
val list = List.empty[AnotherObj]
def findAll = for (obj <- objs) List.concat(list, obj.anotherObjList)
}
我想要 findAll 函数连接来自集合 objs 的对象的列表。
最佳答案
试试这个:
objs.flatMap(_.anotherObjList)
它不使用for
,但这可能是在 Scala 中实现它的最简洁和可读的方式。
关于scala - 连接多个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334318/