What should I do when I get this?

java.lang.ClassCastException: org.apache.commons.collections.set.UnmodifiableSet 
    cannot be cast to scala.collection.immutable.Set
有帮助吗?

解决方案

You can't cast between them, but you should be able to use the scala.collection.JavaConverters to convert java.util.Set (UnmodifiableSet implements this) => mutable.Set, and then call .toSet to convert to an immutable.Set, eg:

import org.apache.commons.collections.set.UnmodifiableSet
import scala.collection.JavaConverters._
val u = new UnmodifiableSet()
val s = u.asScala.toSet
println(s)

see: http://www.scala-lang.org/api/current/index.html#scala.collection.JavaConverters

(Alternatively use JavaConversions and the asScala can happen implicitly - explicit generally better)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top