0

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
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Anton
  • 494
  • 5
  • 19
  • 3
    You should find a corresponding line in the source code and get rid of `asInstanceOf` or `java` cast. – senia Nov 15 '13 at 09:27
  • possible duplicate of [Iterating over Java collections in Scala](http://stackoverflow.com/questions/495741/iterating-over-java-collections-in-scala) – om-nom-nom Nov 15 '13 at 10:52

1 Answers1

1

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)

barnybug
  • 643
  • 1
  • 5
  • 8