0

I have difficulty processing a list a Scala:
Currently I have a list of like this

(List(JString(2437), JString(2445), JString(2428), JString(321)), CompactBuffer((4,1)))

and I would like after processing, the result will look like below:

( (2437, CompactBuffer((4,1))), (2445, CompactBuffer((4,1))), (2428, CompactBuffer((4,1))), (321, CompactBuffer((4,1))) )

Can any body help me with this issue? Thank you very much.

Marc
  • 16,170
  • 20
  • 76
  • 119
Le Kim Trang
  • 369
  • 2
  • 5
  • 17

1 Answers1

1

Try this:

val pair = (List(JString(2437), JString(2445), JString(2428), JString(321)), 
            CompactBuffer((4,1)))

val result = pair._1.map((_, pair._2))

First, pair._1 gets the list from the tuple. Then, map performs the function on each element of the list. The function (_, pair._2) puts the given element from the list in a new tuple together with the second part of the pair tuple.

marstran
  • 26,413
  • 5
  • 61
  • 67
  • Thank you for your help. I having some errors when trying to apply your solution: value _1 is not a member of org.apache.spark.rdd.RDD[(String, Array[(Int, Int)])]. Do you have any idea? – Le Kim Trang Oct 08 '15 at 02:36
  • I see you've now asked this as a new question at http://stackoverflow.com/questions/33005737/error-processing-scala-list. – Seth Tisue Oct 08 '15 at 03:12