views:

1546

answers:

2

I would like to create a java List based on another java Collection eg. Set in Scala.

Why is this not possible? I get a required: scala.this.Int error.

val in: java.util.Set[String] = new java.util.HashSet()
val out : java.util.List[String] = new java.util.ArrayList(in)

This worked however, but doesn't feel right:

val in: java.util.Set[String] = new java.util.HashSet()
val out: List[String] = new java.util.ArrayList()

out.addAll(in.asInstanceOf[java.util.Set[String]])

Thanks!

+1  A: 

You'll need to explicitly pass the ArrayList type variable.

This works fine:

val in = new java.util.HashSet[String]
val out = new java.util.ArrayList[String](in)
Tony Morris
+2  A: 

This works:

val in: java.util.Set[String] = new java.util.HashSet()
val out : java.util.List[String] = new java.util.ArrayList[String](in)

I assume the problem is somehow related to type erasure, as ArrayList is not parametrized as a Scala array would be, but, rather, it's an existential type. This is probably making the type inference impossible.

Daniel