Reputation: 517
I have an object, A that contains another object, B. B contains two strings, stringOne and stringTwo.
Given that I have a list / sequence of A's,
Is it possible to create a map from the list of A's, that results in a HashMap of type A -> Seq[B], where what goes into Seq[B] are B items with identical B.stringOne and B.stringTwo?
A and B are case classes.
Upvotes: 1
Views: 348
Reputation: 2101
Are you looking for something like this:
case class B(s1: String, s2: String)
case class A(b: B)
val ls = List( A(B("1","2")), A(B("3","4")), A(B("5","6")), A(B("1","2")))
ls.groupBy(identity).map({case (k,v) => (k, v.map(_.b))})
//Output: Map(A(B(1,2)) -> List(B(1,2), B(1,2)), A(B(3,4)) -> List(B(3,4)), A(B(5,6)) -> List(B(5,6)))
Upvotes: 1