import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode(includes='id')
class Employee {
int id
String name
}
You can remove constructors as well if @Canonical
AST is used. Canonical also provides @EqualsAndHashCode
, but to add the includes it has to be used separately again.
UPDATE
If the class is not modifiable and you have a list/hasSet then you can use unique
with a closure to perform the uniqueness. Assuming the SolrDocument
mentioned in comment is referred as Employee
and you have the above HashSet with duplicate ids
, then below should work:
empSet.unique { it.id } //this mutates the original list
empSet.unique( false ) { it.id } //this does not mutate the original list