Home > Software engineering >  convert kotlin/java set to scala immutable
convert kotlin/java set to scala immutable

Time:06-23

Id like to convert an existing kotlin/java set, to scala immutable set, using kotlin/java code.

scala.collection.JavaConversions.asScalaSet only gives me the mutable set.

Must do it that way, because I am inheriting from a scala class on another repo, and would not like to insert scala packages and plugins to my project.

CodePudding user response:

A simple answer would be: you cannot. Java/Kotlin collections are inherently mutable, while Scala immutable collections have a completely different implementation on the inside, so they are not allowed to be freely converted from one to the other.

Now you have two options: first, you can use a more generic interface (scala.collection.Set for example), or, second, you can create a new immutable Scala collection and put all the elements into it: scala.collection.immutable.HashSet().concat(yourExistingCollection)

  • Related