Reputation: 347
I have two Lists: dec_tyres
and jan_tyres
:
scala> dec_tyres
res21: List[(String, Int)] = List((007139::21,10), .....
scala> jan_tyres
res22: List[(String, Int)] = List((005179::24,2), .....
scala> dec_tyres.length
res19: Int = 71
scala> jan_tyres.length
res20: Int = 82
Now to join these I'm doing:
val mb = jan_tyres.toMap
val tyres_count = dec_tyres.flatMap{case (ka,va) => mb.get(ka).map(vb => (ka,va,vb))}.toList
Which gives me:
scala> tyres_count
res23: List[(String, Int, Int)] = List((007139::21,10,8), .....
scala> tyres_count.length
res24: Int = 53
This is computing an inner join and giving me only 53 records, where I'm expecting 100 records.
All the records where both of the dec_tyres
and jan_tyres
do not have a common key are scraped. Instead I want that record with the particular key instead a zero as the respective value.
The solution is hence a "Full Outer Join", which I'm really struggling with.
Upvotes: 1
Views: 1977
Reputation: 51271
So you want a (String, Int, Int)
result for every unique String
from the two (String, Int)
input lists?
val janMap = jan_tyres.toMap // for fast lookup
val decMap = dec_tyres.toMap
val tyres_count = (jan_tyres ++ dec_tyres).map(_._1).distinct.map{ k =>
(k, decMap.getOrElse(k, 0), janMap.getOrElse(k, 0))
}
Combine the lists (++
), keep only the key strings (map(_._1)
), remove duplicates (distinct
), change each key (k
) into a tuple using the Map
s to retrieve the Int
values (map{...}
).
Upvotes: 3