Reputation: 4719
I have this class
case class User(var userId: Int, var userName: String, var email:
String, var password: String) {
def this() = this(0, "", "", "")
}
I have two list same User type list teamMembers usrList
I want to concatenate both into one teamMembers. I don't know how to do it so tried as:
teamMembers:::usrList
teamMembers = teamMembers:::usrList
teamMembers++usrList
teamMembers = teamMembers++usrList
Nothing seems to be working. I guess it must be easy. Just having difficulties to understand scala doc. How to concatenate them into one.
Upvotes: 0
Views: 266
Reputation: 20415
Consider the operator ++=
for appending a list to a (mutable) list; for instance for
case class User(var userId: Int = 0,
var userName: String = "",
var email: String = "",
var password: String = "")
and
var teamMembers = List ( User(0), User(1) )
val usrList = List( User(3) )
we have that
teamMembers ++= usrList
and so
teamMembers
List[User] = List(User(0,,,), User(1,,,), User(3,,,))
Another approach involves
teamMembers = List(teamMembers, usrList).flatten
Namely, we create a list of lists and flatten them up onto a list.
Upvotes: 0
Reputation: 52681
It's unclear what you're problem actually is, the usage is pretty straightforward:
val a = List(1,2,3)
val b = List(4,5,6)
val c = a ::: b // List(1, 2, 3, 4, 5, 6)
val d = a ++ b // List(1, 2, 3, 4, 5, 6)
Maybe the thing you're missing is that concatenation produces a new list that is the concatenation of the previous two? Or that you can't reassign vals so you need to assign to a fresh variable name?
Upvotes: 1