Given a Seq
of tuples like:
Seq(
("a",Set(1,2)),
("a",Set(2,3)),
("b",Set(4,6)),
("b",Set(5,6))
)
I would like to groupBy
and then flatMap
the values to obtain something like:
Map(
b -> Set(4, 6, 5),
a -> Set(1, 2, 3)
)
My first implementation is:
Seq(
("a" -> Set(1,2)),
("a" -> Set(2,3)),
("b" -> Set(4,6)),
("b" -> Set(5,6))
) groupBy (_._1) mapValues (_ map (_._2)) mapValues (_.flatten.toSet)
I was wondering if there was a more efficient and maybe simpler way to achieve that result.