Question

I have the list of Scala Tuples2 ,which i have to group them . I currently use the following way to perform it.

var matches:List[Tuple2[String,Int]]
var m = matches.toSeq.groupBy(i=>i._1).map(t=>(t._1,t._2)).toSeq.sortWith(_._2.size>_._2.size).sortWith(_._2.size>_._2.size)

The above grouping gives me Seq[(String,Seq[(String,Int)])] but i would like to have Seq[(String,Seq[Int])]

I would like to know is there any better and efficient way to the same.

Was it helpful?

Solution

First off, some thoughts:

// You should use `val` instead of `var`
var matches: List[Tuple2[String, Int]] = List("a" -> 1, "a" -> 2, "b" -> 3, "c" -> 4, "c" -> 5)
var m = matches
  .toSeq                            // This isn't necessary: it's already a Seq
  .groupBy(i => i._1)
  .map(t => (t._1, t._2))           // This isn't doing anything at all
  .toSeq
  .sortWith(_._2.size > _._2.size)  // `sortBy` will reduce redundancy
  .sortWith(_._2.size > _._2.size)  // Not sure why you have this twice since clearly the 
                                    // second sorting isn't doing anything...

So try this:

val matches: List[Tuple2[String, Int]] = List("a" -> 1, "a" -> 2, "b" -> 3, "c" -> 4, "c" -> 5)
val m: Seq[(String, Seq[Int])] = 
  matches
    .groupBy(_._1)
    .map { case (k, vs) => k -> vs.map(_._2) }  // Drop the String part of the value
    .toVector
    .sortBy(_._2.size)
println(m) // Vector((b,List(3)), (a,List(1, 2)), (c,List(4, 5)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top