Hi I have the following data and want to map it to the first item in the second parameter. So for:
1 -> List((1,11))
1 -> List((1,1), (1,111))
I want:
(1,11)
(1,1)
When this data is in an RDD I can do the following:
scala> val m = sc.parallelize(Seq(11 -> List((1,11)), 1 -> List((1,1),(1,111))))
m: org.apache.spark.rdd.RDD[(Int, List[(Int, Int)])] = ParallelCollectionRDD[198] at parallelize at <console>:47
scala> m.map(_._2.head).collect.foreach(println)
(1,11)
(1,1)
However, when it is in a Map object (result of a groupBy) I get the following:
scala> val m = Map(11 -> List((1,11)), 1 -> List((1,1)))
m: scala.collection.immutable.Map[Int,List[(Int, Int)]] = Map(11 -> List((1,11)), 1 -> List((1,1), (1,111)))
scala> m.map(_._2.head)
res1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1)
When I map to the whole list I get what I would expect, but not when I call head on it
scala> m.map(_._2)
res2: scala.collection.immutable.Iterable[List[(Int, Int)]] = List(List((1,11)), List((1,1), (1,111)))
I can also get the result I want if I do either of the following:
scala> m.map(_._2).map(_.head)
res4: scala.collection.immutable.Iterable[(Int, Int)] = List((1,11), (1,1))
scala> m.values.map(_.head)
res5: Iterable[(Int, Int)] = List((1,11), (1,1))
Could someone explain please what is going on here?