How to use Java Collections.shuffle() on a Scala array? How to use Java Collections.shuffle() on a Scala array? arrays arrays

How to use Java Collections.shuffle() on a Scala array?


java.util.Collections.shuffle(java.util.Arrays.asList(a:_*))

For the above to work correctly, a's element type has to be a subclass of scala.AnyRef (equivalent to java.lang.Object) because Arrays.asList() uses the array passed in as the backing store for the result java.util.List and java.util.List can contain only object references (not primitive values).*

That is also the reason why Collections.shuffle() which shuffles the passed-in java.util.List actually shuffled the array.*

*: See the note below

For example:

scala> val a = Array[java.lang.Integer](1, 2, 3) // note the type parameter                  a: Array[java.lang.Integer] = Array(1, 2, 3)scala> java.util.Collections.shuffle(java.util.Arrays.asList(a:_*))scala> ares43: Array[java.lang.Integer] = Array(1, 3, 2)scala> java.util.Collections.shuffle(java.util.Arrays.asList(a:_*))scala> ares45: Array[java.lang.Integer] = Array(1, 2, 3)

Note: Scala 2.7.5 is used for the above code snippets. Scala 2.8.0 exhibits different behaviors as Daniel demonstrated. To be on the safe side, do not depend on the fact that the array gets shuffled but instead the list that is returned from Arrays.asList() gets shuffled.

scala> val a = Array[java.lang.Integer](1, 2, 3)a: Array[java.lang.Integer] = Array(1, 2, 3)scala> val b = java.util.Arrays.asList(a:_*)b: java.util.List[java.lang.Integer] = [1, 2, 3]scala> java.util.Collections.shuffle(b)scala> bres50: java.util.List[java.lang.Integer] = [2, 1, 3]scala> java.util.Collections.shuffle(b)scala> bres52: java.util.List[java.lang.Integer] = [3, 1, 2]


It seems Scala is doing something different from Java when it comes to varargs. At least, I can't get that array shuffled any way I try. Supposedly, the shuffle on the list would shuffle the array because the list is array-backed. Well, it seems that Scala will create a new array when passing vararg arguments to Java, therefore making the aforementioned example useless.

scala> val b = java.util.Arrays.asList(a: _*)b: java.util.List[java.lang.String] = [a, b, c]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(a, b, c) [a, b, c]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(a, b, c) [c, b, a]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(a, b, c) [a, c, b]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(a, b, c) [b, a, c]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(a, b, c) [a, b, c]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(a, b, c) [c, a, b]

It does works with Ints, despite the claim otherwise, though:

scala> val a = Array(1,2,3)a: Array[Int] = Array(1, 2, 3)scala> val b = java.util.Arrays.asList(a: _*)b: java.util.List[Int] = [1, 2, 3]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(1, 2, 3) [2, 3, 1]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(1, 2, 3) [3, 2, 1]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(1, 2, 3) [3, 2, 1]scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)Array(1, 2, 3) [1, 2, 3]

On Scala 2.8, there's a simpler way:

scala> scala.util.Random.shuffle(a)res32: Sequence[Int] = Array(1, 2, 3)scala> scala.util.Random.shuffle(a)res33: Sequence[Int] = Array(2, 1, 3)scala> scala.util.Random.shuffle(a)res34: Sequence[Int] = Array(3, 2, 1)

Note that, in a Scala fashion, it does not change the original array.


To answer the "what exactly is happening here?" part:

When you say java.util.Arrays.asList(a) you're calling a static Java method which is defined to take a variable number of arguments (the vararg ... syntax in Java):

public static <T> List<T> asList(T... a) 

In Scala when you make the call to asList you're passing in a single Array[String] and not three string parameters. Even though Scala represents T* as Array[T] you need to tell the compiler that you really mean to be passing three parameters, rather than a single parameter which is a List of three things.

Scala has a convenient way to convert your Array[String] to String,String,String: you use the _* symbol as shown in Walter Chang's answer. You can use it whenever you're passing something to a vararg function.

This is outlined on pages 188 and 189 of Programming in Scala.

You'll also see _* in pattern matching to match zero or more elements in a List.