Reputation: 107
Is there an equivalent to Arrays.asList() in Scala?
Or rather, how would you take a String and convert it into an Array, and then a List in Scala?
Any advice would be appreciated.
Upvotes: 1
Views: 3863
Reputation: 170899
One common use of Arrays.asList
is to produce a list containing the given elements:
Arrays.asList(x, y, z);
The Scala equivalent to that is just
Seq(x, y, z)
The other is to turn an existing array into list:
Arrays.asList(array);
In Scala, this is
array.toSeq
(note that I use Seq
instead of List
here; in Scala, List
is a specific implementation, not an interface. Depending on what you want to do with it, some other type can be appropriate).
Or in many cases, nothing at all. Because Array[A]
is implicitly convertible to IndexedSeq[A]
, collection operations can be done directly on it without converting first.
The same applies to String
, with a caveat that operations List
s are good at are quite uncommon for strings, so string.toList
is even less likely to be appropriate.
Upvotes: 4