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
  • 29,484
  • 8
  • 71
  • 131
ScalaNewbie
  • 173
  • 3
  • 11

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,031
  • 8
  • 32
  • 54