Why doesn't Java varargs support collections?

tb189 picture tb189 · Aug 1, 2011 · Viewed 13.2k times · Source

In my Java code I often use the very handy method(Class... args) varargs. As far as I know, they allow you to pass any amount of Class objects or an array of Class[]. Since I also often use the Java collection classes, I am frustrated by the lack of compatibility between both. As a result, I end up doing collection.toArray(), but that has some type safety issues.

So now for the question: why doesn't Java allow instances of Iterable<T> as vararg arguments, as long as the generic type fits the T... type of the vararg? Doesn't everyone use lists, sets, etc. all the time? Is there an easy, type-safe way to provide the conversion from collection to vararg?

Answer

Joachim Sauer picture Joachim Sauer · Aug 1, 2011

The reason is simple: a variable arity parameter is simply an old-school array paramater with some additional metadata that tells the compiler to provide some syntactic sugar (namely, it allows implicit array creation).

So from the perspective of the JVM Object... is pretty much the same as Object[]. Allowing collections as well would require a more invasive change to the JVM (which has no explicit support for collections to date).

Note that if you want to support both ways, then making the collection-based method is probably the better approach:

public void frobnicate(Object... args) {
  frobnicate(Arrays.asList(args));
}

public void frobnicate(Iterable<Object> args) {
  // do stuff
}

The reason for this is that using Arrays.asList() is usually a cheaper operation than Collection.toArray() (because it creates a simple wrapper).