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 <paul.strac...@det.nsw.edu.au>
Reply: users@groovy.apache.org <users@groovy.apache.org>
Date: February 8, 2016 at 15:12:06
To: users@groovy.apache.org <users@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.
**********************************************************************

Reply via email to