1

I need to convert this structure

val seq = Seq(Seq("a","aa"), Seq("b","bb"), Seq("a", "a2"), Seq("b","b2") )

to this Map:

val map2 = Map ( "a" -> Seq("aa","a2"), "b" -> Seq("bb","b2") )

cannot use toMap because it only works with Tuple2 as input. Any ideas how to approach this?

1 Answer 1

2

You can first group by the first item of each sub-seq and then map the resulting grouped values to only keep the second element of subsequences:

Seq(Seq("a","aa"), Seq("b","bb"), Seq("a", "a2"), Seq("b","b2") )
  .groupBy(_(0)) // Map(b -> List(List(b, bb), List(b, b2)), a -> List(List(a, aa), List(a, a2)))
  .mapValues(_.map(_(1))) // Map(b -> List(bb, b2), a -> List(aa, a2))

which returns:

Map(b -> List(bb, b2), a -> List(aa, a2))

Similar: instead of using _(0) and _(1) you could also use .groupBy(_.head).mapValues(_.map(_.last))


The mapValues part can be made a bit more explicit this way:

.mapValues{
  case valueLists => // List(List(b, bb), List(b, b2))
    valueLists.map{
      case List(k, v) => v // List(b, bb) => bb
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. If I use .groupBy(_.head).mapValues(_.map(_.last)) will the order of the keys be preserved? It seems that it doesn't.
As we're producing a Map, the order of keys is not conserved as there isn't a notion of order with Map.
Based on this answer, if instead of a Map you want List((a,List(aa, a2)), (b,List(bb, b2))) (whose keys are ordered as they appeared first), you can add this 2nd step to the produced unorderedMap: Seq(Seq("a","aa"), Seq("b","bb"), Seq("a", "a2"), Seq("b","b2") ).map(_(0)).distinct.map(k => (k, unorderedMap(k))). A foldLeft type of solution might also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.