Search code examples
javascalacollectionsscala-collections

How to convert a java Map to immutable Scala map via java code?


I need to create a immutable Scala map with java code. I have found other things on here about this but nothing that is updated to the current version.

I've tried:

public static <A, B> Map<A, B> toScalaMap(HashMap<A, B> m) {
        return JavaConverters.mapAsScalaMapConverter(m).asScala().toMap(
                Predef.<Tuple2<A, B>>conforms()
        );
}

Solution

  • I changed conforms to $conforms and it now runs find in both Intellij and the command line but Intellij still give a red line under JavaConverters.mapAsScalaMapConverter(m).asScala().toMap( that says cannot access scala.Predef.$less$colon$colon.

    private   <A,B> scala.collection.immutable.Map<A, B> toScalaMap(Map<A, B> m) {
            return JavaConverters.mapAsScalaMapConverter(m).asScala().toMap(
                    Predef.$conforms()
            );
        }