Compare two Maps in Scala

前端 未结 4 1798
刺人心
刺人心 2021-02-06 00:49

Is there any pre-defined function that I can use to compare two Maps based on the key and give me the difference? Right now, I iterate Map1 and foreach key, I check if there is

4条回答
  •  不思量自难忘°
    2021-02-06 00:54

    Try:

    val diff = (m1.keySet -- m2.keySet) ++ (m2.keySet -- m1.keySet)
    

    diff contains the elements that are in m1 and not in m2 and that are in m2 and not in m1.

提交回复
热议问题