Cherry Cherry - 27 days ago 7
Java Question

How pass kotlin collection as varagrs?

At first glance it is needed just convert collection to array and pass it to method but this does not work:

val toTypedArray = Arrays.asList("a", "b").toTypedArray()
Paths.get("", toTypedArray) // <- compilation error here


No workarounds???

Answer Source

If you want to covert it to varargs, you can prefix your array by prepending “*” to the argument:

Paths.get("", *toTypedArray) 

It’s called spreading operator, as I already described here answer I gave.