I am trying to merge case class in a sequences. ie 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)
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}
Silly me. I could do.
outputSeq.distinct.groupBy(_.a)
I am loving scala. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.