I have a Scala array of strings:
val names:Array[String] = something.map(...)
I need to call an Android(java) method that accepts a Collection
public void addAll (Collection<? extends T> collection)
How do I covert the Array to a Collection?
I have a Scala array of strings:
val names:Array[String] = something.map(...)
I need to call an Android(java) method that accepts a Collection
public void addAll (Collection<? extends T> collection)
How do I covert the Array to a Collection?
java.util.Arrays.asList(names: _*)
import collection.JavaConversions._
val namesColl: java.util.Collection[String] = names.toSeq
In the latter approach names array is first converted to Seq[String] and then an implicit conversion located in JavaConversions figures out that Java collection is needed so it applies necessary transformation trasparently. Don't worry, it is constant in time.
Arrays.asList creates one wrapper while toSeq and implicit conversion - two. In both cases there is no copying of an array, hence constant time complexity. So the difference is minimal, but I agree.