开发者

convert unparameterized Java 1.4 Collection to parameterized Scala Sequence

How can I convert a java 1.4 Collection to a Scala Seq?

I am trying to pass a java-collection to a scala method:

import scala.collection.JavaConversions._

// list is a 1.4 java.util.ArrayList
// repository.getDir is Java legacy code
val list = repository.getDir(...)
perform(list)
d开发者_C百科ef perform(entries: List[SVNDirEntry]) = ...

I always receive this error:

type mismatch; found : java.util.Collection[?0] where type ?0 required: List   
[SVNDirEntry]

So I guess I have to create the parameterized Sequence myself as Scala is only able to create an unparameterized Iterable?


First you have to make sure that list has the type java.util.List[SVNDirEntry]. To do this, use a cast:

list.asInstanceOf[java.util.List[SVNDirEntry]]

After that, the implicit conversion will be resolved for you if you import the JavaConversions object. An implicit conversion to a Scala sequence exists in the JavaConversions object. See the following example with a list of strings being passed to a method that expects a Scala sequence:

scala> val jvlist: java.util.List[_] = new java.util.ArrayList[String]
jvlist: java.util.List[_] = []

scala> jvlist.asInstanceOf[java.util.List[String]]
res0: java.util.List[String] = []

scala> import collection.JavaConversions._                                                                          
import collection.JavaConversions._                                                                                 

scala> def perform(scalaseq: Seq[String]) = println("ok")                                    
perform: (scalaseq: scala.collection.Seq[String])Unit                                                   

scala> perform(res0)                                                                                                
ok       

These conversions do not copy the elements - they simply construct a wrapper around a Java collection. Both versions point to the same underlying data. Thus, there is no implicit conversion in JavaConversions to immutable Scala lists from mutable Java lists, because that would enable changing the contents of a Scala collection that is guaranteed to be immutable.

In short - prefer Seq[...] to List[...] when defining parameters for methods if you can live with a less specific interface (as in perform above). Or write your own function that does the conversion by copying the elements.


You have to cast the legacy collection down to the target type. Something along the lines of:

perform(list.asInstanceOf[List[SVNDirEntry]])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜