My intention is simple: to get comma seperated values of emaild from list of User objects. I have done this in Java with a for loop and if else conditions.
现在我想在Schala这样做,这样一来就是尝试。
case class User(name: String, email: String)
val userList = List(User("aaa", "[email protected]"),
User("bbb", "[email protected]"),
User("ccc", "[email protected]"))
现在
val mailIds = userList.foldLeft(""){(a: String, b: User) => ( b.email+","+a) }
发放
[email protected],[email protected],[email protected],
(note the comma at the end.)
以及
val mailids = userList.map(x => x.email).reduceLeft(_+","+_)
发放
[email protected],[email protected],[email protected]
i 仅使用reduceLeft
进行审判 ......
val emailids = userList.reduceLeft((emails: String, user: User) => emails+", "+user.email)
但它投下了汇编错误。
type mismatch; found : (String, User) => java.lang.String required: (java.io.Serializable, User) => java.io.Serializable
因此,在上述情况下,是否有使用<代码>reduceLeft的<><><><>>>>>>><>>> >>>>> ><>>> > > 。