Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On replacing JavaConversions with JavaConverters

Tags:

scala

When I try to run code based on this example, I get the following warning:

warning: object JavaConversions in package collection is deprecated (since 2.12.0): use JavaConverters

AFAICT, the lines responsible for the warning are these:

import scala.collection.JavaConversions._

/* ... */

    for ((k,v) <- environmentVars) println(s"key: $k, value: $v")

Replacing the import line with

import scala.collection.JavaConverters._

...is not enough; doing this alone results in the error:

error: value withFilter is not a member of java.util.Map[String,String]
    for ((k,v) <- environmentVars) println(s"key: $k, value: $v")

What else must be done?

like image 486
kjo Avatar asked Oct 09 '17 15:10

kjo


2 Answers

You need to add the asScala method:

import scala.collection.JavaConverters._

for ((k,v) <- environmentVars.asScala) println(s"key: $k, value: $v")

Since Scala 2.13, this is now CollectionConverters:

import scala.jdk.CollectionConverters._

for ((k,v) <- environmentVars.asScala) println(s"key: $k, value: $v")
like image 194
Yuval Itzchakov Avatar answered Nov 08 '22 22:11

Yuval Itzchakov


FYI, scala.collection.JavaConverters has now been replaced with scala.jdk.CollectionConverters

like image 1
Hugh Avatar answered Nov 08 '22 23:11

Hugh