I'm using some Scala library from my Java code. And I have a problem with collections. I need to pass scala.collection.immutable.Map
as a parameter of a method. I can convert or build immutable.Map
from my Java code but I do not know how to do it. Suggestions?

- 52,967
- 18
- 114
- 136

- 291
- 1
- 3
- 4
7 Answers
It's entirely possible to use JavaConverters
in Java code—there are just a couple of additional hoops to jump through:
import java.util.HashMap;
import scala.Predef;
import scala.Tuple2;
import scala.collection.JavaConverters;
import scala.collection.immutable.Map;
public class ToScalaExample {
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 HashMap<String, String> example() {
HashMap<String, String> m = new HashMap<String, String>();
m.put("a", "A");
m.put("b", "B");
m.put("c", "C");
return m;
}
}
We can show that this works from the Scala REPL:
scala> val jm: java.util.HashMap[String, String] = ToScalaExample.example
jm: java.util.HashMap[String,String] = {b=B, c=C, a=A}
scala> val sm: Map[String, String] = ToScalaExample.toScalaMap(jm)
sm: Map[String,String] = Map(b -> B, c -> C, a -> A)
But of course you could just as easily call these methods from Java code.

- 138,631
- 12
- 375
- 680
-
5There is no "toMap" method any more. The code above can't be compiled for Java 1.7 and Scala 2.11 :( – Alexei Osipov Jan 16 '16 at 15:28
-
30jdk 1.8, it shows "can't access scala.Predef.$less$colon$less" error – Freedom Oct 10 '16 at 10:31
My solution for Java 1.7 and Scala 2.11:
@SuppressWarnings("unchecked")
private static <K, V> scala.collection.immutable.Map<K, V> toScalaImmutableMap(java.util.Map<K, V> javaMap) {
final java.util.List<scala.Tuple2<K, V>> list = new java.util.ArrayList<>(javaMap.size());
for (final java.util.Map.Entry<K, V> entry : javaMap.entrySet()) {
list.add(scala.Tuple2.apply(entry.getKey(), entry.getValue()));
}
final scala.collection.Seq<Tuple2<K, V>> seq = scala.collection.JavaConverters.asScalaBufferConverter(list).asScala().toSeq();
return (scala.collection.immutable.Map<K, V>) scala.collection.immutable.Map$.MODULE$.apply(seq);
}

- 1,154
- 1
- 13
- 36
Since I couldn't make it with Alexey's answer, I'll add that in Scala 2.13 it can be done using:
scala.collection.immutable.Map<Integer, String> scalaMap =
scala.collection.immutable.Map.from(scala.jdk.CollectionConverters.MapHasAsScala(javaMap).asScala());

- 8,413
- 7
- 27
- 35
This worked for me with java 1.8 and scala 2.12:
public static <K, V> scala.collection.immutable.Map<K, V> toScalaImmutableMap(java.util.Map<K, V> jmap) {
List<Tuple2<K, V>> tuples = jmap.entrySet()
.stream()
.map(e -> Tuple2.apply(e.getKey(), e.getValue()))
.collect(Collectors.toList());
Seq<Tuple2<K, V>> scalaSeq = JavaConverters.asScalaBuffer(tuples).toSeq();
return (Map<K, V>) Map$.MODULE$.apply(scalaSeq);
}
Since Scala 2.13 you can use CollectionConverters
to achieve that.
Having
Map<String, String> javaMap = ...
First we convert it to mutable Scala map:
import scala.jdk.javaapi.CollectionConverters$;
var mutableScalaMap = CollectionConverters$.MODULE$.asScala(javaMap);
And then to immutable one:
var scalaMap = scala.collection.immutable.Map$.MODULE$.from(mutableScalaMap);

- 16,718
- 2
- 31
- 40
Can you provide an additional API call that takes/provides a java.util.Map converted using JavaConverters ?
class Example {
import scala.collection.JavaConverters._
def fromMap(m:Map[...]) = ...
// generics etc. elided
def fromJava(m:java.util.Map) = {
fromMap(m.asScala.toMap)
}
}
You may wish to extract the conversion and provide a decorator (especially as I note you're working to a Scala library). Note dhg's comment re. immutability.

- 268,207
- 37
- 334
- 440
-
Yes. Hence my opening question - 'Can you provide an additional API call that takes/provides a java.util.Map' – Brian Agnew Jan 18 '21 at 10:54
Simply use toMap (Scala 2.12)
import scala.collection.JavaConverters._
// asScala creates mutable Scala Map
// toMap after asScala creates immutable Map
val scalaImmutableMap = javaMap.asScala.toMap

- 2,938
- 1
- 28
- 35
-
HI @TomerShetah I might be wrong ,but to convert Java to Scala collection , we should be writing the convertor in scala. – Abhishek Sengupta Jan 17 '21 at 13:09
-
But the question asks for java code, which this is not. So it does not answer the question, regardless if you are right or wrong. – Tomer Shetah Jan 17 '21 at 13:11