在Scala中声明Java Map <string,object =“”>参数?

时间:2017-07-27 14:55:00

标签: scala

鉴于此Java:

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, 
    Map<String, Object> arguments) throws IOException;

从Scala传递null这样的法律是合法的:

channel.queueDeclare(inputQueueName, true, false, true, null)

Scala中传递空映射的声明是什么?这样:

    channel.queueDeclare(inputQueueName, true, false, true, Map[String, Object]())

结果:

found   : scala.collection.immutable.Map[String,Object]
required: java.util.Map[String,Object]
    channel.queueDeclare(inputQueueName, true, false, true, Map[String,Object]())

channel.queueDeclare(inputQueueName, true, false, true, java.util.HashMap[String,Object]())

结果:

object java.util.HashMap is not a value

1 个答案:

答案 0 :(得分:3)

import scala.collection.JavaConverters._

val emptyMap = Map.empty[String, Object].asJava
// java.util.Map[String, Object]

channel.queueDeclare(inputQueueName, true, false, true, emptyMap)

或者:

channel.queueDeclare(inputQueueName, true, false, true, new java.util.HashMap[String, Object]())
                                                     // ^ add new here

new java.util.HashMap[String, Object](没有括号)也有效。