Convert java hashmap to scala map

1.8k views Asked by At

In scala i have function like this:

def generateString(format: String, 
    parameters: Map[String, String] = Map()) {

    generate(format, parameters)    
}

And now, i want to call it form java

Map<String, String> params = new HashMap<String, String>();
params.put("test1", "test2");
Generator.generateString("", params);

But i get error that cannot convert form java map to scala map.

So, how to solve this.

p.s. I am new to Scala.

1

There are 1 answers

1
ursa On

Change your utility signature:

def generateString(format: String, 
    parameters: java.util.Map[String, String] = new java.util.HashMap()) {

Or use java-to-scala converter in Java code:

Generator.generateString("", scala.collection.JavaConversions.mapAsScalaMap(params));