JavaConversions 类型不适用于参数 (Set<String>)

type JavaConversions is not applicable for the arguments (Set<String>)

这是代码片段:

protected static List<String>  myclass(String mydata){

     scala.collection.Set<String> abc = <**some code that returns set of strings**(scala code)>

      List<String> 123 = JavaConversions.<?>(abc)
}

如果您可以将 Set 转换为 Seq,那么代码可以编译为,

protected static List<String>  myclass(String mydata){

     scala.collection.Set<String> abc = <**some code that returns set of strings**(scala code)>

      List<String> 123 = JavaConversions.seqAsJavaList(abc.toSeq)
}

我认为这可能是由于您使用的类型:scala.collection.Set 而不是 scala.collection.immutable.Set。使用接口而不是不可变版本的任何特殊原因? https://www.scala-lang.org/api/2.9.0/scala/collection/Set.html

这对我有用:

scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._

scala> val s = Set("abc","def")
s: scala.collection.immutable.Set[String] = Set(abc, def)

scala> s.asJava
       ^
       warning: object JavaConverters in package collection is deprecated (since 2.13.0): Use `scala.jdk.CollectionConverters` instead
res4: java.util.Set[String] = [abc, def]

或按照警告的建议:

scala> import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters._

scala> val s = Set.of("abc", "def")
                   ^
       error: value of is not a member of object scala.collection.immutable.Set

scala> val s = Set("abc", "def")
s: scala.collection.immutable.Set[String] = Set(abc, def)

scala> s.asJava
asJava   asJavaCollection

scala> s.asJava
res1: java.util.Set[String] = [abc, def]

scala> s.asJavaCollection
res3: java.util.Collection[String] = [abc, def]