Is there a way to easily override toSet(Collection<T> self)
method from DefaultGroovyMethods
?
Its implementation uses HashMap
public static <T> Set<T> toSet(Collection<T> self) {
Set<T> answer = new HashSet<T>(self.size());
answer.addAll(self);
return answer;
}
but I’d like to use a LinkedHashMap
to preserve the order of elements in the argument collection.
I tried with metaprogramming (already used a few times in various custom classes), e.g.
Collection.metaClass.toSet = {
println 'in'
Collection self = (Collection) delegate
Set<Collection> answer = new LinkedHashSet<Collection>(self.size())
answer.addAll(self)
return answer
}
but with no luck (cannot enter that closure).
Any suggestion will be very welcome. Thanks is advance!
ps: I'm currently developing a Grails 4.0.3 app, Groovy 2.5.6