0

First List data as below

List(("A",66729122803169854198650092,"SD"),("B",14941578978240528153321786,"HD"),("C",14941578978240528153321786,"PD"))

and second list contains data as below

List(("X",14941578978240528153321786),("Y",68277588597782900503675727),("Z",14941578978240528153321786),("L"66729122803169854198650092))

using above two list I want to form following list which matched first list second number to second list second number so my output should as below

List(("X",14941578978240528153321786,"B","HD"),("X",14941578978240528153321786,"C","PD"),   ("Y",68277588597782900503675727,"",""),("Z",14941578978240528153321786,"B","HD"),("Z",14941578978240528153321786,"C","PD"),
("L",66729122803169854198650092,"A","SD"))
Pradip Karad
  • 1,377
  • 2
  • 9
  • 8

1 Answers1

2
val tuples3 = List(
  ("A", "66729122803169854198650092", "SD"),
  ("B", "14941578978240528153321786", "HD"),
  ("C", "14941578978240528153321786", "PD"))

val tuples2 = List(
  ("X", "14941578978240528153321786"),
  ("Y", "68277588597782900503675727"),
  ("Z", "14941578978240528153321786"),
  ("L", "66729122803169854198650092"))

Group first list by target field:

val tuples3Grouped =
  tuples3
    .groupBy(_._2)
    .mapValues(_.map(t => (t._1, t._3)))
    .withDefaultValue(List(("", "")))

Zip all data:

val result = for{ (first, second) <- tuples2
     t <- tuples3Grouped(second)
} yield (first, second, t._1, t._2)
Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43