Using scala map in Java

2020-03-24 06:58发布

问题:

I have two files. One is scala and other is java.

Scala file has a function which returns scala immutable map.

Java file wants to use that map as dictionary.

I am a newbie to scala and java. How can I convert that scala map to java dicionary?

回答1:

Use the static forwarders from Java. In Scala 2.13 the API is simplified, with overloaded asJava for conversion to Java:

$ javap -cp ~/scala-2.13.0/lib/scala-library.jar scala.jdk.javaapi.CollectionConverters
Compiled from "CollectionConverters.scala"
public final class scala.jdk.javaapi.CollectionConverters {
  public static scala.collection.mutable.Map<java.lang.String, java.lang.String> asScala(java.util.Properties);
  public static <A, B> scala.collection.mutable.Map<A, B> asScala(java.util.Dictionary<A, B>);
  public static <A, B> scala.collection.concurrent.Map<A, B> asScala(java.util.concurrent.ConcurrentMap<A, B>);
  public static <A, B> scala.collection.mutable.Map<A, B> asScala(java.util.Map<A, B>);
  public static <A> scala.collection.mutable.Set<A> asScala(java.util.Set<A>);
  public static <A> scala.collection.mutable.Buffer<A> asScala(java.util.List<A>);
  public static <A> scala.collection.Iterable<A> asScala(java.util.Collection<A>);
  public static <A> scala.collection.Iterable<A> asScala(java.lang.Iterable<A>);
  public static <A> scala.collection.Iterator<A> asScala(java.util.Enumeration<A>);
  public static <A> scala.collection.Iterator<A> asScala(java.util.Iterator<A>);
  public static <K, V> java.util.concurrent.ConcurrentMap<K, V> asJava(scala.collection.concurrent.Map<K, V>);
  public static <K, V> java.util.Map<K, V> asJava(scala.collection.Map<K, V>);
  public static <K, V> java.util.Dictionary<K, V> asJavaDictionary(scala.collection.mutable.Map<K, V>);
  public static <K, V> java.util.Map<K, V> asJava(scala.collection.mutable.Map<K, V>);
  public static <A> java.util.Set<A> asJava(scala.collection.Set<A>);
  public static <A> java.util.Set<A> asJava(scala.collection.mutable.Set<A>);
  public static <A> java.util.List<A> asJava(scala.collection.Seq<A>);
  public static <A> java.util.List<A> asJava(scala.collection.mutable.Seq<A>);
  public static <A> java.util.List<A> asJava(scala.collection.mutable.Buffer<A>);
  public static <A> java.util.Collection<A> asJavaCollection(scala.collection.Iterable<A>);
  public static <A> java.lang.Iterable<A> asJava(scala.collection.Iterable<A>);
  public static <A> java.util.Enumeration<A> asJavaEnumeration(scala.collection.Iterator<A>);
  public static <A> java.util.Iterator<A> asJava(scala.collection.Iterator<A>);
}

Given a Scala class that returns a Scala Map:

$ jshell --class-path .:~/scala-2.13.0/lib/scala-library.jar 
|  Welcome to JShell -- Version 11.0.3
|  For an introduction type: /help intro

jshell> Map<String, String> vs = scala.jdk.javaapi.CollectionConverters.asJava(new mytools.MapMaker().m())
vs ==> {greeting=hi, reply=bye}


回答2:

This is a better way to convert a Scala immutable.Map to a Java Map in Java.

java.util.Map<String, String> javaMap = scala.collection.JavaConverters
                                           .mapAsJavaMapConverter(scalaMap).asJava();