I currently have 2 lists List('a','b','a') and List(45,65,12) with many more elements and elements in 2nd list linked to elements in first list by having a key value relationship. I want combine elements with same keys by adding their corresponding values and create a map which should look like Map('a'-> 57,'b'->65) as 57 = 45 + 12.
I have currently implemented it as
val keys = List('a','b','a') val values = List(45,65,12) val finalMap:Map(char:Int) = scala.collection.mutable.Map().withDefaultValue(0) 0 until keys.length map (w => finalMap(keys(w)) += values(w)) I feel that there should be a better way(functional way) of creating the desired map than how I am doing it. How could I improve my code and do the same thing in more functional way?
combineByKeylike Apache Spark does.