我有两个列表,其中包含地图列表。
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)
我需要比较每个地图的值,并显示值是否不匹配
输出应该是这样的:
&#34; abcd&#34;的价值list1中的值小于list2
中的值注意:两个列表中的名称相同,但值可能会有所不同
如果我们在groovy中有任何内置函数,请告诉我,我是groovy编程的新手
答案 0 :(得分:0)
//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"
}