4

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()
        );
}
5
  • What's the bigger picture of what you are trying to achieve? Why do you want a Scala map in Java code? Commented Jul 11, 2017 at 19:28
  • I am working with the kafka AdminClient class which returns a collections.scala.immutable.Map and I want to be able to create one so I can have a mock return it in a unit test. Commented Jul 11, 2017 at 19:35
  • so, what happened when you tried it? Commented Jul 11, 2017 at 19:51
  • I get cannot find symbol: Predef.<Tuple2<A, B>>conforms() Commented Jul 11, 2017 at 19:54
  • Try scala.Predef.conforms<Tuple2<A,B>>() Commented Jul 11, 2017 at 20:34

4 Answers 4

5

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()
        );
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot man. It worked ! Wondering why Intellij throws that error
2

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);
    }
}

Comments

0

I ran into this issue with IntelliJ, but it turned out to be just an IntelliJ compiler error and was able to run via maven just fine. Try running/building your code to see if its just a compiler error.

Comments

0

JavaConverters are deprecated since 2.13.0
https://www.scala-lang.org/api/2.13.4/scala/collection/JavaConverters$.html

Deprecated (Since version 2.13.0) Use scala.jdk.CollectionConverters instead

Here is a solution with scala.jdk.CollectionConverters:

import scala.jdk.CollectionConverters;

...

public static <A, B> scala.collection.immutable.Map<A, B> toScalaMap(Map<A, B> theMap) {
    return scala.collection.immutable.Map.from(CollectionConverters.MapHasAsScala(theMap).asScala());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.