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?
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?
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)