I'm looking to append a val adminEmailSeq = Seq.empty[String]
from a List of object attribute.
My List[User]
called 'admins' and I'm trying to do this, but it doesn't work:
admins.foreach(
admin => {
adminEmailSeq :+ admin.email
}
)
Although admin.email
contains the right information, adminEmailSeq.isEmpty
is always true.
根据说明,我假设您需要Admins
Emails
val adminEmailSeq = admins.map(_.email)
The append ':+' actually doesn't append it to the leading Seq. It's making a copy. Regarding to your solution you would need to do this.
admins.foreach(
admin => {
adminEmailSeq = adminEmailSeq :+ admin.email
}
)
But I think the right solution would be using map.
Just forgot: I'm a fan of immutables and values instead of variables. It mayhelp to understand the code much easier. Therefore I wouldn't use that variable reassigning (therefore I suggested map, as some solutions here may show you).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.