Your code is correct. The problem is likely an issue with how you're building or running your code, or perhaps you don't have all the necessary imports. The following works with version 4.6.1 of the Scala IDE for Eclipse and Scala 2.12:
package org.soreadytohelp;
import java.util.HashMap;
import scala.Predef;
import scala.Tuple2;
import scala.collection.JavaConverters;
import scala.collection.immutable.Map;
public class MapTest {
public static <A, B> Map<A, B> toScalaMap(HashMap<A, B> m) {
return JavaConverters.mapAsScalaMapConverter(m).asScala().toMap(
Predef.<Tuple2<A, B>>conforms()
);
}
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("food", "bacon");
Map<String, String> hmAsScala = toScalaMap(hm);
System.out.println(hmAsScala);
}
}
scala.Predef.conforms<Tuple2<A,B>>()