Scala Map Merge

http://www.nimrodstech.com/scala-map-merge/

Need to merge multiple maps by their keys and make manipulation on the values? this post is probably for you!
Why ?previous experience with map reduce, many times data collected was in a Map structure, found myself repeatedly summing/appending/listing/manipulating maps

Assuming we have 2 maps with names of people as the key and the amount of them as the value :

 //each map contains count of people names
 val names = Map("Sidney" -> 1, "Paul" -> 1, "Jacob" -> 7)
 val moreNames = Map("Sidney" -> 1, "Paul" -> 5, "Nick" -> 2)

For some reason we need to merge the 2 maps into 1 expecting to sum the values and get all keys.

 //need to merge them to 1 map that sums all people names
 //expecting : Map("Sidney" -> 2, "Paul" -> 6, "Nick" -> 2, "Jacob" -> 7)

Lets write our own implementation for this task :

 //note ++ on maps will get all keys with second maps values if exists, if not first map values.
 val mergedMap = names ++ moreNames.map {
    case (name,count) => name -> (count + names.getOrElse(name,0)) }

Next task: lets merge the maps but instead of summing the values lets list them

 //expecting : Map("Sidney" -> List(1,1), "Paul" -> List(5,1), "Nick" -> List(2), "Jacob" -> List(7))
 val mergedMap2 = (names.toSeq ++ moreNames.toSeq)
  .groupBy{case(name,amount) => name}
  .mapValues(person => person.map{ case(name,amount) => amount}.toList)

Ok that was nice, but can be achieved much easier using Scalaz semigroups

//in your projects dependencies
val scalazDep = Seq (org.scalaz %% scalaz-core % 7.0.6)
 //now lets use scalaz Semigroup concept
 //lets merge maps by key and sum the values !
 import scalaz.Scalaz._
 val mergedMap3 = names |+| moreNames
 //lets merge maps by key and list the values
 val mergedMap4 = names.map(p => p._1 -> List(p._2)) |+| moreNames.map(p => p._1 -> List(p._2))

Main idea with using scalaz |+| is to arrange the data structure to what you need to do. in case you need to sum values a int will do the job. case you need to list the values a list of Any will do the trick.
This example really comes in handy when used in reduce functions, resulting in clean and short code (if you aren’t a scalaz hater)

 //each map contains count of people names
 val names = Map("Sidney" -> 1, "Paul" -> 1, "Jacob" -> 7)
 val moreNames = Map("Sidney" -> 1, "Paul" -> 5, "Nick" -> 2)
 //Money time!
 val test = List(names , moreNames).reduce(_ |+| _)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,017評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,641評論 5 6
  • 我,一個畢業(yè)于,211工程大學的本科生,從一開始參加工作。一直到六年后的今天,我的窗外,沒有變化過,或者,一直在重...
    11號樓閱讀 323評論 0 0
  • 夢 你笑 說無知 不可辦到 我不理不睬 在似水流年間 我選了風雨照料 不愿做命運的農(nóng)奴 在風雨中學會了站立 冥冥中...
    程xi閱讀 217評論 1 0
  • 殺人的心,有想,不敢; 自殺的心,有想,也不敢; 終究是缺了份跟死有關(guān)的勇氣。 ———————————— 文明人活...
    smile絲嘜小主閱讀 307評論 0 0

友情鏈接更多精彩內(nèi)容