Don Branson Don Branson - 2 years ago 129
Scala Question

Declare Java Map<String, Object> arguments in Scala?

Given this Java:

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


It's legal to pass
null
like this from Scala:

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


What's the declaration in Scala to pass in an empty Map? This:

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


Results in:

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


And:

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


Results in:

object java.util.HashMap is not a value

Answer Source
import scala.collection.JavaConverters._

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

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

Or:

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

new java.util.HashMap[String, Object] (without the parentheses) also works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download