You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Strachan, Paul" <Pa...@det.nsw.edu.au> on 2016/02/08 15:11:48 UTC

not sure about Collection.intersect

Groovy 2.4.4 / 2.4.5

Hi - I'd like to get a list of objects from collection A that exist in collection B using intersect() but I'm getting no results:


def c1 = []// as Set
def c2 = []// as Set
c1 << new TestClass(name: 'mike')
c2 << new TestClass(name: 'mike')
println c1.contains(c2[0])
assert c1.intersect(c2).size() == 1




Output:

true
Assertion failed:

assert c1.intersect(c2).size() == 1
       |  |         |   |      |
       |  []        |   0      false
       |            [sample.TestClass@333357]
       [sample.TestClass@333357]


TestClass.groovy


package sample

import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode(includes = 'name')
class TestClass {
    String name
}


Is intersect only for simple types?



**********************************************************************
This message is intended for the addressee named and may contain
privileged information or confidential information or both. If you
are not the intended recipient please delete it and notify the sender.
**********************************************************************

Re: not sure about Collection.intersect

Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
Hi Paul,

c1 contains one instance of TestClass, c2 contains another. Those two are not equals, because they probably do not implement the equals method, and thus comparison is done between object references in memory, and they are different, being two different objects.

if you equals was implemented to compare the name of TestClass I'm pretty sure it would work.

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Strachan, Paul <pa...@det.nsw.edu.au>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: February 8, 2016 at 15:12:06
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject:  not sure about Collection.intersect  

Groovy 2.4.4 / 2.4.5

 

Hi – I’d like to get a list of objects from collection A that exist in collection B using intersect() but I’m getting no results:

 

def c1 = []// as Set
def c2 = []// as Set
c1 << new TestClass(name: 'mike')
c2 << new TestClass(name: 'mike')
println c1.contains(c2[0])
assert c1.intersect(c2).size() == 1
  
  
Output:

 

true

Assertion failed:

 

assert c1.intersect(c2).size() == 1

       |  |         |   |      |

       |  []        |   0      false

       |            [sample.TestClass@333357]

       [sample.TestClass@333357]

 

 

TestClass.groovy

 

package sample
import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode(includes = 'name')
class TestClass {
    String name
}

 

 

Is intersect only for simple types?

 

 


**********************************************************************
This message is intended for the addressee named and may contain
privileged information or confidential information or both. If you
are not the intended recipient please delete it and notify the sender.
**********************************************************************

RE: not sure about Collection.intersect

Posted by "Strachan, Paul" <Pa...@det.nsw.edu.au>.
Changing to list did not help
@EqualsAndHashCode(includes = ['name'])

possibly I need to reboot, turn off the lights and go to bed :)



From: Edinson E. Padrón Urdaneta [mailto:edinson.padron.urdaneta@gmail.com]
Sent: Tuesday, 9 February 2016 1:41 AM
To: users@groovy.apache.org
Subject: Re: not sure about Collection.intersect


Could it be that you're using a String instead of a List of String as the value for the `includes` element?

Check this: http://docs.groovy-lang.org/next/html/gapi/groovy/transform/EqualsAndHashCode.html#includes

**********************************************************************
This message is intended for the addressee named and may contain
privileged information or confidential information or both. If you
are not the intended recipient please delete it and notify the sender.
**********************************************************************

Re: not sure about Collection.intersect

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
Could it be that you're using a String instead of a List of String as the
value for the `includes` element?

Check this:
http://docs.groovy-lang.org/next/html/gapi/groovy/transform/EqualsAndHashCode.html#includes

Re: not sure about Collection.intersect

Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
And by the way,

the easy way to implement equals and haschode is this:

import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode
class TestClass {
    String name
}

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Edinson E. Padrón Urdaneta <ed...@gmail.com>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: February 8, 2016 at 15:29:00
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject:  Re: not sure about Collection.intersect  

I have to look at the implementation of the `intersect` method to be sure but does your TestClass class overwrite `hashcode` and `equal`? There should be a way to compare the instances of said class.

Re: not sure about Collection.intersect

Posted by Paul King <pa...@asert.com.au>.
Yes, I think the fix proposed in:

https://issues.apache.org/jira/browse/GROOVY-7530

will fix this bug with intersect too. See also:

https://issues.apache.org/jira/browse/GROOVY-7602

Cheers, Paul.


On Tue, Feb 9, 2016 at 1:32 PM, Strachan, Paul
<Pa...@det.nsw.edu.au> wrote:
> OK. It appears to me like a Groovy bug introduced in version 2.3.10 –
> possibly https://issues.apache.org/jira/browse/GROOVY-7267
>
>
>
> def c1 = []
> def c2 = []
>
> c1 << new URL("http://sample.com/")
> c2 << new URL("http://sample.com/")
> println c1.intersect(c2).size()
>
>
>
> In Groovy <= 2.3.9 the output is 1
>
> In Groovy > 2.3.9 the output is 0
>
>
>
>
>
>
>
> From: Edinson E. Padrón Urdaneta [mailto:edinson.padron.urdaneta@gmail.com]
> Sent: Tuesday, 9 February 2016 1:29 AM
> To: users@groovy.apache.org
> Subject: Re: not sure about Collection.intersect
>
>
>
> I have to look at the implementation of the `intersect` method to be sure
> but does your TestClass class overwrite `hashcode` and `equal`? There should
> be a way to compare the instances of said class.
>
>
> **********************************************************************
> This message is intended for the addressee named and may contain
> privileged information or confidential information or both. If you
> are not the intended recipient please delete it and notify the sender.
> **********************************************************************

RE: not sure about Collection.intersect

Posted by "Strachan, Paul" <Pa...@det.nsw.edu.au>.
OK. It appears to me like a Groovy bug introduced in version 2.3.10 – possibly https://issues.apache.org/jira/browse/GROOVY-7267

def c1 = []
def c2 = []
c1 << new URL("http://sample.com/")
c2 << new URL("http://sample.com/")
println c1.intersect(c2).size()

In Groovy <= 2.3.9 the output is 1
In Groovy > 2.3.9 the output is 0



From: Edinson E. Padrón Urdaneta [mailto:edinson.padron.urdaneta@gmail.com]
Sent: Tuesday, 9 February 2016 1:29 AM
To: users@groovy.apache.org
Subject: Re: not sure about Collection.intersect


I have to look at the implementation of the `intersect` method to be sure but does your TestClass class overwrite `hashcode` and `equal`? There should be a way to compare the instances of said class.

**********************************************************************
This message is intended for the addressee named and may contain
privileged information or confidential information or both. If you
are not the intended recipient please delete it and notify the sender.
**********************************************************************

Re: not sure about Collection.intersect

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
Oh! I'm sorry, I don't know why I didn't notice the class's code.

Re: not sure about Collection.intersect

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
I have to look at the implementation of the `intersect` method to be sure
but does your TestClass class overwrite `hashcode` and `equal`? There
should be a way to compare the instances of said class.