groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Søren Berg Glasius (GR8Conf EU)" <>
Subject Re: not sure about Collection.intersect
Date Mon, 08 Feb 2016 14:26:48 GMT
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:, 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 <>
Reply: <>
Date: February 8, 2016 at 15:12:06
To: <>
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



Assertion failed:


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

       |  |         |   |      |

       |  []        |   0      false

       |            [sample.TestClass@333357]






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.
View raw message