简体   繁体   中英

how can i get a string of an element of a list of classes in scala

I have a List of a class where the class is defined like:

case class Role (role_id, elem2, elem3)

well sort of...

so if I have a List of those as roles: List[Role]

how can I get a string of the role_id's so that if my list had 4 Roles in it my string might look like "3 6 8 9" ?

or better still how can i add some string to it so i can get "3, 6, 8, 9" ?

im having to craft some sql and want set based operations instead of looping. I feel i should flatten or something but i cant think

Thank you Martin

Try something like this:

scala> case class Role(role_id: Int, elem2: String, elem3: String)
defined class Role

scala> val l = List(Role(1, "", ""), Role(2, "", ""), Role(3, "", ""))
l: List[Role] = List(Role(1,,), Role(2,,), Role(3,,))

scala> l.map({ case Role(id, _, _) => id }).mkString(", ")
res2: String = 1, 2, 3

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM