简体   繁体   中英

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

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

//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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM