Frage

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
War es hilfreich?

Lösung

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)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top