I want to create a List with 10 different objects of a certain class in Groovy. In Java there is Arrays.fill but I'm certain there is a much more elegant way in Groovy.
I tried this:
def mylist = [new MyClass()]*10;
But that gives me the same reference for every Item in the List.
I ended up using @cfrick's version:
def l = ([MyClass]*10)*.newInstance()
assert l.size() == 10
assert l.unique().size() == 10
It fits on one line and is readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With