Le Kim Trang
Le Kim Trang

Reputation: 459

Merge two arrays based on first element

I have 2 arrays Array[(Int, Int)], and Array[(Int, List[String])], for examples:

    (1, 2) and (1, (123, 456, 789)) 
    (2, 8) and (2, (678, 1000))
    (3, 4) and (3, (587, 923, 168, 392))

I would like to merge these two arrays into one Array [(Int, List[String], Int)] like this:

    (1, (123, 456, 789), 2)
    (2, (678, 1000), 8)
    (3, (587, 923, 168, 392), 4)

and would like scala still realize the second element is a List[String], I tried many ways they can combine these 2 maps or arrays, but cannot realize the second element is a List[String], after merging, it treated the second element as Any or Some and cannot traverse it.

Upvotes: 1

Views: 269

Answers (1)

Le Kim Trang
Le Kim Trang

Reputation: 459

I found the solution:

array1.zip(array2).map { 
     case ((p1,count), (p2,categoryList)) => (p1,categoryList,count) 
}

Upvotes: 3

Related Questions