[英]Comare the list of Maps with their values in Groovy
I have two lists which contain list of maps in it. 我有两个列表,其中包含地图列表。
List<Map<String,String>> list1=new ArrayList<Map<String,String>>()
List<Map<String,String>> list2=new ArrayList<Map<String,String>>()
def m1 = [name: 'abcd', value:'1.2.3']
def m2 = [name: 'xyx', value:'4.6.3']
list1.add(m1)
list1.add(m2)
def m3 = [name: 'abcd', value:'1.2.6']
def m4 = [name: 'xyx', value:'4.6.9']
list2.add(m3)
list2.add(m4)
I need to compare the values of each map and display if the values are mismatched 我需要比较每个地图的值并显示值是否不匹配
the output should be something like this: 输出应该是这样的:
the Value of the "abcd" from the list1 is Lesser than the value from the list2 list1中的“ abcd”的值小于list2中的值
Note:The names in the two lists are same but values may vary 注意:两个列表中的名称相同,但值可能有所不同
Please let me know if we have any built in functions in the groovy,I am new to the groovy programming 请让我知道groovy中是否有任何内置函数,我是groovy编程的新手
//i think better to have just a map for this task for faster access
//you can convert your list of maps to map: list1.collectEntries{[it.name,it.value]}
def map1 = [:]
map1['abcd'] = '1.2.3'
map1['xyx'] = '4.6.3'
def map2 = [:]
map2['abcd'] = '1.2.6'
map2['xyx'] = '4.6.3'
map1.each{k1,v1->
def v2 = map2[k1]
if(v1!=v2) println "the value of $k1 in list1 `$v1` is "+( v1>v2 ? "greater" : "less" ) +" then `$v2` in list2"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.