[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: [groovy-user] Re: How to compare two nested maps.
From: Dinesh <dinez.v () gmail ! com>
Date: 2013-04-26 11:53:29
Message-ID: 70E1EECA-E73E-42CA-B8E7-877CB0B2A703 () gmail ! com
[Download RAW message or body]
That's awesome. Thanks everyone...
On 26 ಎಪ್ರಿಲ್ 2013, at 05:34 AM, "paulk_asert [via Groovy]" \
<ml-node+s329449n5715186h45@n5.nabble.com> 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_viewe \
> > > > r&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNames \
> > > > pace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNam \
> > > > espace&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
--
View this message in context: \
http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715190.html
Sent from the groovy - user mailing list archive at Nabble.com.
[Attachment #3 (text/html)]
<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br>That's \
awesome. Thanks everyone...</div><div><br>On 26 ಎಪ್ರಿಲ್ 2013, at 05:34 \
AM, "paulk_asert [via Groovy]" <<a \
href="/user/SendEmail.jtp?type=node&node=5715190&i=0" target="_top" rel="nofollow" \
link="external">[hidden email]</a>> wrote:<br><br></div><blockquote \
style='border-left:2px solid #CCCCCC;padding:0 1em' type="cite"><div>
<br>+1, much nicer
<br><br>On 26/04/2013 6:42 PM, Dinko Srkoc wrote:
<div class="shrinkable-quote"><div class='shrinkable-quote'><br>> One can also use \
multimethods to break down checks by type. IMO, it <br>> makes the code a bit more \
clear, and avoids `instanceof`: <br>>
<br>> def equiv(Map a, Map b) {
<br>> a.size() == b.size() &&
<br>> a.every { k, v -> b.containsKey(k) \
&& equiv(v, b[k]) } <br>> }
<br>>
<br>> def equiv(List a, List b) {
<br>> a.toSet() == b.toSet()
<br>> }
<br>>
<br>> def equiv(a, b) {
<br>> a == b
<br>> }
<br>>
<br>> Cheers,
<br>> Dinko
<br>>
<br>> On 26 April 2013 06:24, Paul King <<a \
href="/user/SendEmail.jtp?type=node&node=5715186&i=0" target="_top" \
rel="nofollow" link="external">[hidden email]</a>> wrote: <br>>>
<br>>> Yes, calling recursively would be appropriate.
<br>>>
<br>>> def equiv(val1, val2) {
<br>>> if (val1 instanceof List && val2 instanceof List) {
<br>>> return val1.toSet() == val2.toSet()
<br>>> }
<br>>> if (val1 instanceof Map && val2 instanceof Map) {
<br>>> if (val1.size() != val2.size()) return false
<br>>> return val1.every { k, v ->
<br>>> if (!val2.containsKey(k)) return false
<br>>> if (!equiv(v, val2[k])) return false
<br>>> true
<br>>> }
<br>>> }
<br>>> val1 == val2
<br>>> }
<br>>>
<br>>> Cheers, Paul.
<br>>>
<br>>>
<br>>>
<br>>> On 26/04/2013 12:32 PM, Dinesh wrote:
<br>>>>
<br>>>> Thank you very much Paul...
<br>>>>
<br>>>> I have some other cases where I have list, string, integer inside a \
map, <br>>>> where this is not working... I have modified a little bit as \
shown below ( <br>>>> the code should take care of nested conditions \
too...). Do i need to do it <br>>>> recursively? kindly advise...
<br>>>>
<br>>>> In the below map2, the map entry aMap has "arr" key whose oder is
<br>>>> different.
<br>>>>
<br>>>> def map1 = ["num":123, "bool":true, "str":"Hello", \
"aMap":["num":123, <br>>>> "bool":false, "str":"hello", "bMap":["num":123, \
"bool":false, <br>>>> "str":"hello"],"arr": ["one","two"]]]
<br>>>> def map2 = ["num":123, "str":"Hello", "bool":true, \
"aMap":["num":123, <br>>>> "bool":false, "str":"hello", "bMap":["num":123, \
"bool":false, <br>>>> "str":"hello"],"arr": ["two","one"]]]
<br>>>>
<br>>>> def areMapsEquiv(map1,map2){
<br>>>> map1.every {k, v ->
<br>>>> if ( \
!map2.containsKey(k)) return false <br>>>> \
if ( v instanceof List ) { <br>>>> \
if (v.toSet() != \
map2[k].toSet()) return false <br>>>> \
} <br>>>> \
else{ <br>>>> \
if ( v != map2[k] ) return false \
<br>>>> \
} <br>>>> \
true <br>>>> \
} <br>>>> }
<br>>>>
<br>>>> areMapsEquiv (map1,map2)
<br>>>>
<br>>>>
<br>>>> On Thu, Apr 25, 2013 at 7:17 PM, paulk_asert [via Groovy] \
<[hidden email] <br>>>> \
</user/SendEmail.jtp?type=node&node=5715178&i=0>> wrote: \
<br>>>> <br>>>>
<br>>>> You don't give much background but here is one \
way that might be <br>>>> close to what you need:
<br>>>>
<br>>>> def areMapsEquiv(map1, map2) {
<br>>>> if (map1.size() != map2.size()) \
return false <br>>>> map1.every { k, v \
-> <br>>>> if \
(!map2.containsKey(k)) return false <br>>>> \
if (k == 'LA' && v.toSet() != map2[k].toSet()) return false \
<br>>>> if (k != 'LA' && v \
!= map2[k]) return false <br>>>> \
true <br>>>> }
<br>>>> }
<br>>>>
<br>>>> Of course if you can you would just store sets \
in your data structure. <br>>>> Then you would just use \
"map1 == map2". <br>>>>
<br>>>> Cheers, Paul.
<br>>>>
<br>>>> On 26/04/2013 8:11 AM, Dinesh wrote:
<br>>>>
<br>>>> > Ok, but to compare those maps...
<br>>>> >
<br>>>> > Thanks...
<br>>>> >
<br>>>> > On Apr 25, 2013, at 6:05 PM, "tim yates-2 \
[via Groovy]" <[hidden <br>>>> email] \
</user/SendEmail.jtp?type=node&node=5715176&i=0>> wrote: \
<br>>>> > <br>>>> \
>> The problem is those aren't nested maps, they're lists of maps. \
<br>>>> >> <br>>>> \
>> So you need something that says [ 1, 2 ] == [ 2, 1 ] <br>>>> \
>> <br>>>> >> On 25 Apr 2013 \
22:46, "Dinesh" <[hidden email] <br>>>> \
</user/SendEmail.jtp?type=node&node=5715175&i=0>> wrote: \
<br>>>> <br>>>> >>
<br>>>> >> Hi,
<br>>>> >>
<br>>>> >> I am new to Groovy and I \
need a groovy method to compare <br>>>> nested maps:
<br>>>> >> Below are the samples - \
both maps are same (only the order of <br>>>> the maps in
<br>>>> >> the list is different) \
however, equals on the returns false. <br>>>> Can anybody
<br>>>> >> tell me how to compare \
these kind of nested maps by ignoring <br>>>> the orders in
<br>>>> >> groovy.
<br>>>> >>
<br>>>> >> def map1 =
<br>>>> >>
<br>>>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"tom","number":5],["name":"hank","number":3]]]
<br>>>> >> def map2 =
<br>>>> >>
<br>>>> ["BA":[],"ow":"a5I","Code":"300","LA":[["name":"hank","number":3],["name":"tom","number":5]]]
<br>>>> >>
<br>>>> >> Thanks...
<br>>>>
<br>>>> \
--------------------------------------------------------------------- \
<br>>>> To unsubscribe from this list, please visit: \
<br>>>> <br>>>> <a \
href="http://xircles.codehaus.org/manage_email" target="_top" rel="nofollow" \
link="external">http://xircles.codehaus.org/manage_email</a><br>>>> \
<br>>>> <br>>>>
<br>>>>
<br>>>>
<br>>>> --------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
---------------------------------------------------------------------------------------------------------------------------------------------------
<br>>>>
<br>>>> If you reply to this email, your message will be \
added to the <br>>>> discussion below:
<br>>>>
<br>>>> <a href="http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html" \
target="_top" rel="nofollow" \
link="external">http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715177.html</a><br>>>> \
To unsubscribe from How to compare two nested maps., click here. \
<br>>>> NAML <br>>>> <<a \
href="http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&a \
mp;id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespac \
e-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" \
target="_top" rel="nofollow" \
link="external">http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macr \
o_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Bas \
icNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeName \
space&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml</a>>
<br>>>>
<br>>>>
<br>>>>
<br>>>>
<br>>>> --------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
-------------------------------------------------------------------------------------- \
---------------------------------------------------------------------------------------------------------------------------------------------------
<br>>>> View this message in context: Re: How to compare two nested maps.
<br>>>> <<a \
href="http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715178.html" \
target="_top" rel="nofollow" \
link="external">http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715178.html</a>>
<br>>>> Sent from the groovy - user mailing list archive
<br>>>> <<a \
href="http://groovy.329449.n5.nabble.com/groovy-user-f329450.html" target="_top" \
rel="nofollow" link="external">http://groovy.329449.n5.nabble.com/groovy-user-f329450.html</a>> \
at <a href="http://Nabble.com" target="_top" rel="nofollow" \
link="external">Nabble.com</a>. <br>>>
<br>>>
<br>>>
<br>>> ---------------------------------------------------------------------
<br>>> To unsubscribe from this list, please visit:
<br>>>
<br>>> <a href="http://xircles.codehaus.org/manage_email" \
target="_top" rel="nofollow" \
link="external">http://xircles.codehaus.org/manage_email</a><br>>> <br>>>
<br>>
<br>> ---------------------------------------------------------------------
<br>> To unsubscribe from this list, please visit:
<br>>
<br>> <a href="http://xircles.codehaus.org/manage_email" \
target="_top" rel="nofollow" \
link="external">http://xircles.codehaus.org/manage_email</a><br>> <br>>
<br>>
</div></div><br>---------------------------------------------------------------------
<br>To unsubscribe from this list, please visit:
<br><br> <a href="http://xircles.codehaus.org/manage_email" \
target="_top" rel="nofollow" \
link="external">http://xircles.codehaus.org/manage_email</a><br><br><br>
<br>
<br>
<hr noshade="noshade" size="1" color="#cccccc">
<div style="color:#444; font: 12px tahoma,geneva,helvetica,arial,sans-serif;">
<div style="font-weight:bold">If you reply to this email, your message will be \
added to the discussion below:</div> <a \
href="http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715186.html" \
target="_top" rel="nofollow" \
link="external">http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715186.html</a>
</div>
<div style="color:#666; font: 11px \
tahoma,geneva,helvetica,arial,sans-serif;margin-top:.4em;line-height:1.5em">
To unsubscribe from How to compare two nested maps., <a href="" target="_top" \
rel="nofollow" link="external">click here</a>.<br> <a \
href="http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&a \
mp;id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespac \
e-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" \
rel="nofollow" style="font:9px serif" target="_top" link="external">NAML</a> \
</div></div></blockquote>
<br/><hr align="left" width="300" />
View this message in context: <a \
href="http://groovy.329449.n5.nabble.com/How-to-compare-two-nested-maps-tp5715174p5715190.html">Re: \
How to compare two nested maps.</a><br/> Sent from the <a \
href="http://groovy.329449.n5.nabble.com/groovy-user-f329450.html">groovy - user \
mailing list archive</a> at Nabble.com.<br/>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic