Use scala.collection.JavaConversions
for implicit conversions between Scala and Java collections.
In the following approach we convert a Java HashSet
onto a Scala Set
, extract keys of interest, and convert the result onto a new Java HashSet
:
import scala.collection.JavaConversions._
val javaKeys = new java.util.HashSet[Any](CalltoJavaAPI())
val n = javaKeys.size
val scalaSet = javaKeys.toSet.take(n-1)
val newJavaHashSet = new java.util.HashSet[Any]()
newJavaHashSet.addAll(scalaSet)