Collection<Foo> foos = ...; Foo[] foosToAdd = ...; foos.addAll(Arrays.asList(foosToAdd);
What I hadn't noticed is that the
java.util.Collections
class has a static method that does this:Collection<Foo> foos = ...; Foo[] foosToAdd = ...; Collections.addAll(foos, foosToAdd);
Which is not only a little bit simpler but the javadoc says is faster for most implementations of
Collection
.Better yet,
Collections.addAll
takes a variable length argument list and not just a straight array so you can do without the array declaration and just do:Collection<Foo> foos = ...; Collections.addAll(foos, foo1, foo2, etc);
See: Collections.addAll Javadoc
2 comments:
Nice, thank you! :)
Thanks!
Post a Comment