tags:

views:

27

answers:

1

As simple as this must be I still can't understand where am I wrong:

class A {
    boolean equals(o) { true }
}
def s = [new A(), new A()] as Set
assert s.size() == 1 // Assertion failed: actually gives 2

Which method should I override in order to get uniqueness?

+2  A: 

hashCode, as you would with Java

class A {
    boolean equals(o) { true }
    int hashCode() { 1 }
}
def s = [new A(), new A()] as Set
assert s.size() == 1
tim_yates