Reputation: 4038
I am trying to merge case class in a sequences. i.e I have the following case class:
case class Output(a: String , b: String, c: String, d: Int)
val outputSeq = Seq(
Output("serviceA","targetA","8000",0),
Output("serviceA","targetA","8080",1),
Output("serviceA","targetA","8000",0)
)
I would like to achieve the following output:
Map(serviceA -> List(Output(serviceA, targetA, 8080, 0), Output(serviceA, targetA, 8080, 1))
following gives me the map but I am stuck how to get rid of duplicates. I know I need to further filter using map but I am a noob in scala FP.
outputSeq.groupBy(_.a)
Upvotes: 1
Views: 158
Reputation: 51271
Since mapValues()
has been deprecated (as of Scala 2.13.0) it's recommended that we use the long form.
outputSeq.groupBy(_.a)
.map{case (k,vs) => k -> vs.distinct}
Upvotes: 3
Reputation: 4038
Silly me. I could do.
outputSeq.distinct.groupBy(_.a)
I am loving scala. :)
Upvotes: 2