比较 Scala 中的两个 Map [英] Compare two Maps in Scala
本文介绍了比较 Scala 中的两个 Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何预定义的函数可用于根据键比较两个 Map 并给出差异?现在,我迭代 Map1 和 foreach 键,我检查 Map2 中是否有一个元素,并通过模式匹配找到差异.有没有更优雅的方法来做到这一点?
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 an element in Map2 and I pattern match to find the difference. Is there a much elegant way to do this?
推荐答案
考虑转换成元组集合的映射之间的差异,
Consider the difference between the maps converted into sets of tuples,
(m1.toSet diff m2.toSet).toMap
这篇关于比较 Scala 中的两个 Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文