Groovy web console

subscribe to the feed Subscribe
to this
site
set replace (via #groovywebconsole)
tweet this snippet Tweet
this
script

set replace

Published 7 months ago by Gabriel Bianchi with tags set replace add remove
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class A {
   int id
   int other

   @Override
   public boolean equals(Object o) {
       o instanceof A && o.id == this.id
   }

   @Override
   public int hashCode() {
       this.id
   }

   @Override
   public String toString() {
      "${id} + ${other}".toString()
   }
}

Collection<A> a = [] as Set<A>

a.add(new A(id: 1, other: 2))
println a
a.add(new A(id: 2, other: 2))
println a
a.add(new A(id: 1, other: 3))
println a
a.remove(new A(id: 1, other: 2))
a.add(new A(id: 1, other: 3))
println a