6

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?

3
  • Is your project in Java or Scala? Commented Feb 1, 2014 at 6:59
  • Its in scala. I am using sbt for build Commented Feb 1, 2014 at 7:03
  • 1
    As a note, you should never use Dictionary or Hashtable in new code unless interfacing with ancient code that requires it. Use the Map interface (and usually HashMap) instead. Commented Feb 1, 2014 at 8:18

2 Answers 2

8

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

Comments

6

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 Comments

I am using the library which is written in scala. And my code is in java. And I dont want any bloated helper functions in scala files.
Thank you very much for the answer. Where to get the info on these types of syntax? Any leads ?

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.