Converting a Scala typed collection to Java with unlimited wildcard, etc. Collection <?>

Problem: It is necessary to implement an interface from a third-party Java library in Scala

...

Collection<?> getItemPropertyIds()

...

My solution is to use ...<here goes Iterable>.asInstanceOf[java.util.Collection[_]]

 val props:Map[Object,Property] = ...
 override def getItemPropertyIds()=props.keys.asInstanceOf[java.util.Collection[_]]

Is there a better solution? Maybe with Predef implied?

+3
source share
2 answers

Create scala. Use, use scala.collection.asJavaCollection () (may be implicit) to convert to java.util.Collection.

+1
source

I will also try:

import scala.collection.JavaConversions
...
override def getItemPropertyIds() = JavaConversions.asCollection(props.keys)
+1
source

Source: https://habr.com/ru/post/1758886/


All Articles