I have the following code:
private lazy val keys: List[String] = obj.getKeys().asScala.toList
obj.getKeys
returns a java.uti
That would work if obj.getKeys() was a java.util.Iterator
. I suppose it is not.
If obj.getKeys()
is just java.util.Iterator
in raw form, not java.util.Iterator
, not even java.util.Iterator>
, this is something scala tend to dislikes, but anyway, there is no way scala will type your expression as List[String]
if it has no guarantee obj.getKeys()
contains String.
If you know your iterator is on Strings, but the type does not say so, you may cast :
obj.getKeys().asInstanceOf[java.util.Iterator[String]]
(then go on with .asScala.toList
)
Note that, just as in java and because of type erasure, that cast will not be checked (you will get a warning). If you want to check immediately that you have Strings, you may rather do
obj.getKeys().map(_.asInstanceOf[String])
which will check the type of each element while you iterate to build the list