2

I have a list of objects in Scala. Let's say:

list: List[ClassX] = List(objectA, objectB, objectC)

and the class

class ClassX{

  var attrA
  var attrB

}

Is there a predefined method call, if I want to get attrA of all objects in my list?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
ScalaNewbie
  • 173
  • 3
  • 12

1 Answers1

4

Use map:

val as = list.map(_.attrA)

as is a List[A], where A is the type of attrA in ClassX.

The above is a shorthand notation for:

val as = list.map(a => a.attrA)
Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54