用JavaConverters替换JavaConversions

时间:2017-10-09 15:06:28

标签: scala

当我尝试运行基于this example的代码时,我收到以下警告:

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

AFAICT,负责警告的行是:

import scala.collection.JavaConversions._

/* ... */

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

替换import
import scala.collection.JavaConverters._

......还不够;单独这样做会导致错误:

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

还有什么必须做的?

2 个答案:

答案 0 :(得分:7)

您需要添加asScala方法:

import scala.collection.JavaConverters._

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

答案 1 :(得分:1)

仅供参考,scala.collection.JavaConverters已替换为scala.jdk.CollectionConverters