连接多个列表

jer*_*ome 3 collections scala

我想知道如何使用循环连接几个列表.这是我正在尝试做的一个例子:

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)
}
Run Code Online (Sandbox Code Playgroud)

我想函数findAll连接集合objs的对象列表.

Jea*_*let 5

试试这个:

objs.flatMap(_.anotherObjList)
Run Code Online (Sandbox Code Playgroud)

它不使用a for,但这可能是Scala中最简洁易读的方法.