Why doesn’t Java varargs support collections?

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).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)