Quantcast

How to compare two nested maps.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to compare two nested maps.

Dinesh
Hi,

I am new to Groovy and I need a groovy method to compare nested maps:
Below are the samples - both maps are same (only the order of the maps in the list is different) however, equals on the returns false. Can anybody tell me how to compare these kind of nested maps by ignoring the orders in groovy.

def map1 = ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
def map2 = ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]

Thanks...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

tim yates-2

The problem is those aren't nested maps, they're lists of maps.

So you need something that says [ 1, 2 ] == [ 2, 1 ]

On 25 Apr 2013 22:46, "Dinesh" <[hidden email]> wrote:
Hi,

I am new to Groovy and I need a groovy method to compare nested maps:
Below are the samples - both maps are same (only the order of the maps in
the list is different) however, equals on the returns false. Can anybody
tell me how to compare these kind of nested maps by ignoring the orders in
groovy.

def map1 =
["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
def map2 =
["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]

Thanks...




--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

Dinesh
Ok, but to compare those maps...

Thanks...

On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden email]> wrote:

The problem is those aren't nested maps, they're lists of maps.

So you need something that says [ 1, 2 ] == [ 2, 1 ]

On 25 Apr 2013 22:46, "Dinesh" <[hidden email]> wrote:
Hi,

I am new to Groovy and I need a groovy method to compare nested maps:
Below are the samples - both maps are same (only the order of the maps in
the list is different) however, equals on the returns false. Can anybody
tell me how to compare these kind of nested maps by ignoring the orders in
groovy.

def map1 =
["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
def map2 =
["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]

Thanks...




--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





If you reply to this email, your message will be added to the discussion below:
http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715175.html
To unsubscribe from How to compare two nested maps., click here.
NAML
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

paulk_asert

You don't give much background but here is one way that might be
close to what you need:

def areMapsEquiv(map1, map2) {
   if (map1.size() != map2.size()) return false
   map1.every { k, v ->
     if (!map2.containsKey(k)) return false
     if (k == 'LA' && v.toSet() != map2[k].toSet()) return false
     if (k != 'LA' && v != map2[k]) return false
     true
   }
}

Of course if you can you would just store sets in your data structure.
Then you would just use "map1 == map2".

Cheers, Paul.

On 26/04/2013 8:11 AM, Dinesh wrote:

> Ok, but to compare those maps...
>
> Thanks...
>
> On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden email] </user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote:
>
>> The problem is those aren't nested maps, they're lists of maps.
>>
>> So you need something that says [ 1, 2 ] == [ 2, 1 ]
>>
>> On 25 Apr 2013 22:46, "Dinesh" <[hidden email] </user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote:
>>
>>     Hi,
>>
>>     I am new to Groovy and I need a groovy method to compare nested maps:
>>     Below are the samples - both maps are same (only the order of the maps in
>>     the list is different) however, equals on the returns false. Can anybody
>>     tell me how to compare these kind of nested maps by ignoring the orders in
>>     groovy.
>>
>>     def map1 =
>>     ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
>>     def map2 =
>>     ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
>>
>>     Thanks...


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

Dinesh
Thank you very much Paul... 

I have some other cases where I have list, string, integer inside a map, where this is not working... I have modified a little bit as shown below  ( the code should take care of nested conditions too...). Do i need to do it recursively? kindly advise...

In the below map2, the map entry aMap has "arr" key whose oder is different.

def map1 = ["num":123, "bool":true, "str":"Hello", "aMap":["num":123, "bool":false, "str":"hello", "bMap":["num":123, "bool":false, "str":"hello"],"arr": ["one","two"]]]
def map2 = ["num":123, "str":"Hello", "bool":true, "aMap":["num":123, "bool":false, "str":"hello", "bMap":["num":123, "bool":false, "str":"hello"],"arr": ["two","one"]]]

def areMapsEquiv(map1,map2){
    map1.every {k, v ->
                if ( !map2.containsKey(k)) return false
                if ( v instanceof List ) {
                   if (v.toSet() != map2[k].toSet()) return false
                }
                else{          
                    if ( v != map2[k] ) return false
                 }
                 true
                }
    
}

areMapsEquiv (map1,map2)


On Thu, Apr 25, 2013 at 7:17 PM, paulk_asert [via Groovy] <[hidden email]> wrote:

You don't give much background but here is one way that might be
close to what you need:

def areMapsEquiv(map1, map2) {
   if (map1.size() != map2.size()) return false
   map1.every { k, v ->
     if (!map2.containsKey(k)) return false
     if (k == 'LA' && v.toSet() != map2[k].toSet()) return false
     if (k != 'LA' && v != map2[k]) return false
     true
   }
}

Of course if you can you would just store sets in your data structure.
Then you would just use "map1 == map2".

Cheers, Paul.

On 26/04/2013 8:11 AM, Dinesh wrote:

> Ok, but to compare those maps...
>
> Thanks...
>
> On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden email] </user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote:
>
>> The problem is those aren't nested maps, they're lists of maps.
>>
>> So you need something that says [ 1, 2 ] == [ 2, 1 ]
>>
>> On 25 Apr 2013 22:46, "Dinesh" <[hidden email] </user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote:

>>
>>     Hi,
>>
>>     I am new to Groovy and I need a groovy method to compare nested maps:
>>     Below are the samples - both maps are same (only the order of the maps in
>>     the list is different) however, equals on the returns false. Can anybody
>>     tell me how to compare these kind of nested maps by ignoring the orders in
>>     groovy.
>>
>>     def map1 =
>>     ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
>>     def map2 =
>>     ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
>>
>>     Thanks...

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





If you reply to this email, your message will be added to the discussion below:
http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html
To unsubscribe from How to compare two nested maps., click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

paulk_asert

Yes, calling recursively  would be appropriate.

def equiv(val1, val2) {
   if (val1 instanceof List && val2 instanceof List) {
     return val1.toSet() == val2.toSet()
   }
   if (val1 instanceof Map && val2 instanceof Map) {
     if (val1.size() != val2.size()) return false
     return val1.every { k, v ->
       if (!val2.containsKey(k)) return false
       if (!equiv(v, val2[k])) return false
       true
     }
   }
   val1 == val2
}

Cheers, Paul.


On 26/04/2013 12:32 PM, Dinesh wrote:

> Thank you very much Paul...
>
> I have some other cases where I have list, string, integer inside a map, where this is not working... I have modified a little bit as shown below  ( the code should take care of nested conditions too...). Do i need to do it recursively? kindly advise...
>
> In the below map2, the map entry aMap has "arr" key whose oder is different.
>
> def map1 = ["num":123, "bool":true, "str":"Hello", "aMap":["num":123, "bool":false, "str":"hello", "bMap":["num":123, "bool":false, "str":"hello"],"arr": ["one","two"]]]
> def map2 = ["num":123, "str":"Hello", "bool":true, "aMap":["num":123, "bool":false, "str":"hello", "bMap":["num":123, "bool":false, "str":"hello"],"arr": ["two","one"]]]
>
> def areMapsEquiv(map1,map2){
>      map1.every {k, v ->
>                  if ( !map2.containsKey(k)) return false
>                  if ( v instanceof List ) {
>                     if (v.toSet() != map2[k].toSet()) return false
>                  }
>                  else{
>                      if ( v != map2[k] ) return false
>                   }
>                   true
>                  }
> }
>
> areMapsEquiv (map1,map2)
>
>
> On Thu, Apr 25, 2013 at 7:17 PM, paulk_asert [via Groovy] <[hidden email] </user/SendEmail.jtp?type=node&node=5715178&i=0>> wrote:
>
>
>     You don't give much background but here is one way that might be
>     close to what you need:
>
>     def areMapsEquiv(map1, map2) {
>         if (map1.size() != map2.size()) return false
>         map1.every { k, v ->
>           if (!map2.containsKey(k)) return false
>           if (k == 'LA' && v.toSet() != map2[k].toSet()) return false
>           if (k != 'LA' && v != map2[k]) return false
>           true
>         }
>     }
>
>     Of course if you can you would just store sets in your data structure.
>     Then you would just use "map1 == map2".
>
>     Cheers, Paul.
>
>     On 26/04/2013 8:11 AM, Dinesh wrote:
>
>      > Ok, but to compare those maps...
>      >
>      > Thanks...
>      >
>      > On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden email] </user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote:
>      >
>      >> The problem is those aren't nested maps, they're lists of maps.
>      >>
>      >> So you need something that says [ 1, 2 ] == [ 2, 1 ]
>      >>
>      >> On 25 Apr 2013 22:46, "Dinesh" <[hidden email] </user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote:
>
>      >>
>      >>     Hi,
>      >>
>      >>     I am new to Groovy and I need a groovy method to compare nested maps:
>      >>     Below are the samples - both maps are same (only the order of the maps in
>      >>     the list is different) however, equals on the returns false. Can anybody
>      >>     tell me how to compare these kind of nested maps by ignoring the orders in
>      >>     groovy.
>      >>
>      >>     def map1 =
>      >>     ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
>      >>     def map2 =
>      >>     ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
>      >>
>      >>     Thanks...
>
>     ---------------------------------------------------------------------
>     To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
>
>     ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>     If you reply to this email, your message will be added to the discussion below:
>     http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html
>     To unsubscribe from How to compare two nested maps., click here.
>     NAML <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
>
>
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> View this message in context: Re: How to compare two nested maps. <http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715178.html>
> Sent from the groovy - user mailing list archive <http://groovy.329449.n5.nabble.com/groovy-user-f329450.html> at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

Dinko Srkoč
One can also use multimethods to break down checks by type. IMO, it
makes the code a bit more clear, and avoids `instanceof`:

    def equiv(Map a, Map b) {
        a.size() == b.size() &&
        a.every { k, v -> b.containsKey(k) && equiv(v, b[k]) }
    }

    def equiv(List a, List b) {
        a.toSet() == b.toSet()
    }

    def equiv(a, b) {
        a == b
    }

Cheers,
Dinko

On 26 April 2013 06:24, Paul King <[hidden email]> wrote:

>
> Yes, calling recursively  would be appropriate.
>
> def equiv(val1, val2) {
>   if (val1 instanceof List && val2 instanceof List) {
>     return val1.toSet() == val2.toSet()
>   }
>   if (val1 instanceof Map && val2 instanceof Map) {
>     if (val1.size() != val2.size()) return false
>     return val1.every { k, v ->
>       if (!val2.containsKey(k)) return false
>       if (!equiv(v, val2[k])) return false
>       true
>     }
>   }
>   val1 == val2
> }
>
> Cheers, Paul.
>
>
>
> On 26/04/2013 12:32 PM, Dinesh wrote:
>>
>> Thank you very much Paul...
>>
>> I have some other cases where I have list, string, integer inside a map,
>> where this is not working... I have modified a little bit as shown below  (
>> the code should take care of nested conditions too...). Do i need to do it
>> recursively? kindly advise...
>>
>> In the below map2, the map entry aMap has "arr" key whose oder is
>> different.
>>
>> def map1 = ["num":123, "bool":true, "str":"Hello", "aMap":["num":123,
>> "bool":false, "str":"hello", "bMap":["num":123, "bool":false,
>> "str":"hello"],"arr": ["one","two"]]]
>> def map2 = ["num":123, "str":"Hello", "bool":true, "aMap":["num":123,
>> "bool":false, "str":"hello", "bMap":["num":123, "bool":false,
>> "str":"hello"],"arr": ["two","one"]]]
>>
>> def areMapsEquiv(map1,map2){
>>      map1.every {k, v ->
>>                  if ( !map2.containsKey(k)) return false
>>                  if ( v instanceof List ) {
>>                     if (v.toSet() != map2[k].toSet()) return false
>>                  }
>>                  else{
>>                      if ( v != map2[k] ) return false
>>                   }
>>                   true
>>                  }
>> }
>>
>> areMapsEquiv (map1,map2)
>>
>>
>> On Thu, Apr 25, 2013 at 7:17 PM, paulk_asert [via Groovy] <[hidden email]
>> </user/SendEmail.jtp?type=node&node=5715178&i=0>> wrote:
>>
>>
>>     You don't give much background but here is one way that might be
>>     close to what you need:
>>
>>     def areMapsEquiv(map1, map2) {
>>         if (map1.size() != map2.size()) return false
>>         map1.every { k, v ->
>>           if (!map2.containsKey(k)) return false
>>           if (k == 'LA' && v.toSet() != map2[k].toSet()) return false
>>           if (k != 'LA' && v != map2[k]) return false
>>           true
>>         }
>>     }
>>
>>     Of course if you can you would just store sets in your data structure.
>>     Then you would just use "map1 == map2".
>>
>>     Cheers, Paul.
>>
>>     On 26/04/2013 8:11 AM, Dinesh wrote:
>>
>>      > Ok, but to compare those maps...
>>      >
>>      > Thanks...
>>      >
>>      > On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden
>> email] </user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote:
>>      >
>>      >> The problem is those aren't nested maps, they're lists of maps.
>>      >>
>>      >> So you need something that says [ 1, 2 ] == [ 2, 1 ]
>>      >>
>>      >> On 25 Apr 2013 22:46, "Dinesh" <[hidden email]
>> </user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote:
>>
>>      >>
>>      >>     Hi,
>>      >>
>>      >>     I am new to Groovy and I need a groovy method to compare
>> nested maps:
>>      >>     Below are the samples - both maps are same (only the order of
>> the maps in
>>      >>     the list is different) however, equals on the returns false.
>> Can anybody
>>      >>     tell me how to compare these kind of nested maps by ignoring
>> the orders in
>>      >>     groovy.
>>      >>
>>      >>     def map1 =
>>      >>
>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
>>      >>     def map2 =
>>      >>
>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
>>      >>
>>      >>     Thanks...
>>
>>     ---------------------------------------------------------------------
>>     To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>>     If you reply to this email, your message will be added to the
>> discussion below:
>>
>> http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html
>>     To unsubscribe from How to compare two nested maps., click here.
>>     NAML
>> <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>> View this message in context: Re: How to compare two nested maps.
>> <http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715178.html>
>> Sent from the groovy - user mailing list archive
>> <http://groovy.329449.n5.nabble.com/groovy-user-f329450.html> at Nabble.com.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

paulk_asert

+1, much nicer

On 26/04/2013 6:42 PM, Dinko Srkoc wrote:

> One can also use multimethods to break down checks by type. IMO, it
> makes the code a bit more clear, and avoids `instanceof`:
>
>      def equiv(Map a, Map b) {
>          a.size() == b.size() &&
>          a.every { k, v -> b.containsKey(k) && equiv(v, b[k]) }
>      }
>
>      def equiv(List a, List b) {
>          a.toSet() == b.toSet()
>      }
>
>      def equiv(a, b) {
>          a == b
>      }
>
> Cheers,
> Dinko
>
> On 26 April 2013 06:24, Paul King <[hidden email]> wrote:
>>
>> Yes, calling recursively  would be appropriate.
>>
>> def equiv(val1, val2) {
>>    if (val1 instanceof List && val2 instanceof List) {
>>      return val1.toSet() == val2.toSet()
>>    }
>>    if (val1 instanceof Map && val2 instanceof Map) {
>>      if (val1.size() != val2.size()) return false
>>      return val1.every { k, v ->
>>        if (!val2.containsKey(k)) return false
>>        if (!equiv(v, val2[k])) return false
>>        true
>>      }
>>    }
>>    val1 == val2
>> }
>>
>> Cheers, Paul.
>>
>>
>>
>> On 26/04/2013 12:32 PM, Dinesh wrote:
>>>
>>> Thank you very much Paul...
>>>
>>> I have some other cases where I have list, string, integer inside a map,
>>> where this is not working... I have modified a little bit as shown below  (
>>> the code should take care of nested conditions too...). Do i need to do it
>>> recursively? kindly advise...
>>>
>>> In the below map2, the map entry aMap has "arr" key whose oder is
>>> different.
>>>
>>> def map1 = ["num":123, "bool":true, "str":"Hello", "aMap":["num":123,
>>> "bool":false, "str":"hello", "bMap":["num":123, "bool":false,
>>> "str":"hello"],"arr": ["one","two"]]]
>>> def map2 = ["num":123, "str":"Hello", "bool":true, "aMap":["num":123,
>>> "bool":false, "str":"hello", "bMap":["num":123, "bool":false,
>>> "str":"hello"],"arr": ["two","one"]]]
>>>
>>> def areMapsEquiv(map1,map2){
>>>       map1.every {k, v ->
>>>                   if ( !map2.containsKey(k)) return false
>>>                   if ( v instanceof List ) {
>>>                      if (v.toSet() != map2[k].toSet()) return false
>>>                   }
>>>                   else{
>>>                       if ( v != map2[k] ) return false
>>>                    }
>>>                    true
>>>                   }
>>> }
>>>
>>> areMapsEquiv (map1,map2)
>>>
>>>
>>> On Thu, Apr 25, 2013 at 7:17 PM, paulk_asert [via Groovy] <[hidden email]
>>> </user/SendEmail.jtp?type=node&node=5715178&i=0>> wrote:
>>>
>>>
>>>      You don't give much background but here is one way that might be
>>>      close to what you need:
>>>
>>>      def areMapsEquiv(map1, map2) {
>>>          if (map1.size() != map2.size()) return false
>>>          map1.every { k, v ->
>>>            if (!map2.containsKey(k)) return false
>>>            if (k == 'LA' && v.toSet() != map2[k].toSet()) return false
>>>            if (k != 'LA' && v != map2[k]) return false
>>>            true
>>>          }
>>>      }
>>>
>>>      Of course if you can you would just store sets in your data structure.
>>>      Then you would just use "map1 == map2".
>>>
>>>      Cheers, Paul.
>>>
>>>      On 26/04/2013 8:11 AM, Dinesh wrote:
>>>
>>>       > Ok, but to compare those maps...
>>>       >
>>>       > Thanks...
>>>       >
>>>       > On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden
>>> email] </user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote:
>>>       >
>>>       >> The problem is those aren't nested maps, they're lists of maps.
>>>       >>
>>>       >> So you need something that says [ 1, 2 ] == [ 2, 1 ]
>>>       >>
>>>       >> On 25 Apr 2013 22:46, "Dinesh" <[hidden email]
>>> </user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote:
>>>
>>>       >>
>>>       >>     Hi,
>>>       >>
>>>       >>     I am new to Groovy and I need a groovy method to compare
>>> nested maps:
>>>       >>     Below are the samples - both maps are same (only the order of
>>> the maps in
>>>       >>     the list is different) however, equals on the returns false.
>>> Can anybody
>>>       >>     tell me how to compare these kind of nested maps by ignoring
>>> the orders in
>>>       >>     groovy.
>>>       >>
>>>       >>     def map1 =
>>>       >>
>>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
>>>       >>     def map2 =
>>>       >>
>>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
>>>       >>
>>>       >>     Thanks...
>>>
>>>      ---------------------------------------------------------------------
>>>      To unsubscribe from this list, please visit:
>>>
>>>      http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>>
>>>
>>> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>>
>>>      If you reply to this email, your message will be added to the
>>> discussion below:
>>>
>>> http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html
>>>      To unsubscribe from How to compare two nested maps., click here.
>>>      NAML
>>> <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>>
>>>
>>>
>>>
>>> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>> View this message in context: Re: How to compare two nested maps.
>>> <http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715178.html>
>>> Sent from the groovy - user mailing list archive
>>> <http://groovy.329449.n5.nabble.com/groovy-user-f329450.html> at Nabble.com.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare two nested maps.

Dinesh

That's awesome. Thanks everyone...

On 26 ಎಪ್ರಿಲ್ 2013, at 05:34 AM, "paulk_asert [via Groovy]" <[hidden email]> wrote:


+1, much nicer

On 26/04/2013 6:42 PM, Dinko Srkoc wrote:

> One can also use multimethods to break down checks by type. IMO, it
> makes the code a bit more clear, and avoids `instanceof`:
>
>      def equiv(Map a, Map b) {
>          a.size() == b.size() &&
>          a.every { k, v -> b.containsKey(k) && equiv(v, b[k]) }
>      }
>
>      def equiv(List a, List b) {
>          a.toSet() == b.toSet()
>      }
>
>      def equiv(a, b) {
>          a == b
>      }
>
> Cheers,
> Dinko
>
> On 26 April 2013 06:24, Paul King <[hidden email]> wrote:
>>
>> Yes, calling recursively  would be appropriate.
>>
>> def equiv(val1, val2) {
>>    if (val1 instanceof List && val2 instanceof List) {
>>      return val1.toSet() == val2.toSet()
>>    }
>>    if (val1 instanceof Map && val2 instanceof Map) {
>>      if (val1.size() != val2.size()) return false
>>      return val1.every { k, v ->
>>        if (!val2.containsKey(k)) return false
>>        if (!equiv(v, val2[k])) return false
>>        true
>>      }
>>    }
>>    val1 == val2
>> }
>>
>> Cheers, Paul.
>>
>>
>>
>> On 26/04/2013 12:32 PM, Dinesh wrote:
>>>
>>> Thank you very much Paul...
>>>
>>> I have some other cases where I have list, string, integer inside a map,
>>> where this is not working... I have modified a little bit as shown below  (
>>> the code should take care of nested conditions too...). Do i need to do it
>>> recursively? kindly advise...
>>>
>>> In the below map2, the map entry aMap has "arr" key whose oder is
>>> different.
>>>
>>> def map1 = ["num":123, "bool":true, "str":"Hello", "aMap":["num":123,
>>> "bool":false, "str":"hello", "bMap":["num":123, "bool":false,
>>> "str":"hello"],"arr": ["one","two"]]]
>>> def map2 = ["num":123, "str":"Hello", "bool":true, "aMap":["num":123,
>>> "bool":false, "str":"hello", "bMap":["num":123, "bool":false,
>>> "str":"hello"],"arr": ["two","one"]]]
>>>
>>> def areMapsEquiv(map1,map2){
>>>       map1.every {k, v ->
>>>                   if ( !map2.containsKey(k)) return false
>>>                   if ( v instanceof List ) {
>>>                      if (v.toSet() != map2[k].toSet()) return false
>>>                   }
>>>                   else{
>>>                       if ( v != map2[k] ) return false
>>>                    }
>>>                    true
>>>                   }
>>> }
>>>
>>> areMapsEquiv (map1,map2)
>>>
>>>
>>> On Thu, Apr 25, 2013 at 7:17 PM, paulk_asert [via Groovy] <[hidden email]
>>> </user/SendEmail.jtp?type=node&node=5715178&i=0>> wrote:
>>>
>>>
>>>      You don't give much background but here is one way that might be
>>>      close to what you need:
>>>
>>>      def areMapsEquiv(map1, map2) {
>>>          if (map1.size() != map2.size()) return false
>>>          map1.every { k, v ->
>>>            if (!map2.containsKey(k)) return false
>>>            if (k == 'LA' && v.toSet() != map2[k].toSet()) return false
>>>            if (k != 'LA' && v != map2[k]) return false
>>>            true
>>>          }
>>>      }
>>>
>>>      Of course if you can you would just store sets in your data structure.
>>>      Then you would just use "map1 == map2".
>>>
>>>      Cheers, Paul.
>>>
>>>      On 26/04/2013 8:11 AM, Dinesh wrote:
>>>
>>>       > Ok, but to compare those maps...
>>>       >
>>>       > Thanks...
>>>       >
>>>       > On Apr 25, 2013, at 6:05 PM, "tim yates-2 [via Groovy]" <[hidden
>>> email] </user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote:
>>>       >
>>>       >> The problem is those aren't nested maps, they're lists of maps.
>>>       >>
>>>       >> So you need something that says [ 1, 2 ] == [ 2, 1 ]
>>>       >>
>>>       >> On 25 Apr 2013 22:46, "Dinesh" <[hidden email]
>>> </user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote:
>>>
>>>       >>
>>>       >>     Hi,
>>>       >>
>>>       >>     I am new to Groovy and I need a groovy method to compare
>>> nested maps:
>>>       >>     Below are the samples - both maps are same (only the order of
>>> the maps in
>>>       >>     the list is different) however, equals on the returns false.
>>> Can anybody
>>>       >>     tell me how to compare these kind of nested maps by ignoring
>>> the orders in
>>>       >>     groovy.
>>>       >>
>>>       >>     def map1 =
>>>       >>
>>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
>>>       >>     def map2 =
>>>       >>
>>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
>>>       >>
>>>       >>     Thanks...
>>>
>>>      ---------------------------------------------------------------------
>>>      To unsubscribe from this list, please visit:
>>>
>>>      http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>>
>>>
>>> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>>
>>>      If you reply to this email, your message will be added to the
>>> discussion below:
>>>
>>> http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html
>>>      To unsubscribe from How to compare two nested maps., click here.
>>>      NAML
>>> <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>>
>>>
>>>
>>>
>>> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>> View this message in context: Re: How to compare two nested maps.
>>> <http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715178.html>
>>> Sent from the groovy - user mailing list archive
>>> <http://groovy.329449.n5.nabble.com/groovy-user-f329450.html> at Nabble.com.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





If you reply to this email, your message will be added to the discussion below:
http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715186.html
To unsubscribe from How to compare two nested maps., click here.
NAML
Loading...