views:

2175

answers:

3

A Java API returns a java.util.Map<java.lang.String,java.lang.Boolean>;. I would like to put that into a Map[String,Boolean]

So imagine we have:

var scalaMap : Map[String,Boolean] = Map.empty
val javaMap = new JavaClass().map()   // Returns java.util.Map<java.lang.String,java.lang.Boolean>

You can't do Map.empty ++ javaMap, because the ++ method does not know about Java maps. I tried:

scalaMap = Map.empty ++ new collection.jcl.MapWrapper[String,Boolean] {
    override def underlying = javaMap
}

and:

scalaMap = Map.empty ++ new collection.jcl.MapWrapper[java.lang.String,java.lang.Boolean] {
    override def underlying = javaMap
  }

These both fail to compile, because of the generics - java.lang.String is not the same as a scala String.

Is there a good way of doing this, short of copying the map manually?

EDIT: Thanks, all good answers, I learned a lot from all of them. However, I made a mistake by posting a simpler problem here than the one I actually have. So, if you allow me, I'll generalise the question - What the API actually returns is

java.util.Map<java.lang.String, java.util.Map<SomeJavaEnum,java.lang.String>>

And I need to move this to Map[String, Map[SomeJavaEnum,String]]

It probably does not seem like too much of a complication, but it adds an extra level of type erasure, and the only way I found of moving this to a Scala map was deep-copying it (using some of the techniques you suggested below). Anyone any hints? I kind of solved my problem by defining an implicit conversion for my exact types, so at least the ugliness is hidden in its own trait, but still feels a bit clumsy deep copying the lot.

A: 

useJavaMap.scala

import test._
import java.lang.Boolean
import java.util.{Map => JavaMap}
import collection.jcl.MapWrapper

object useJavaMap {
  def main(args: Array[String]) {
    var scalaMap : Map[String, Boolean] = Map.empty
    scalaMap = toMap(test.testing())
    println(scalaMap)
  }

  def toMap[K, E](m: JavaMap[K, E]): Map[K, E] = {
    Map.empty ++ new MapWrapper[K, E]() {
      def underlying = m
    }
  }
}

test/test.java

package test;

import java.util.*;

public class test {
    public static Map<String, Boolean> testing() {
     Map<String, Boolean> x = new HashMap<String, Boolean>();
     x.put("Test",Boolean.FALSE);
     return x;
    }
    private test() {}
}

Commandline

javac test\test.java
scalac useJavaMap.scala
scala useJavaMap
> Map(Test -> false)
jitter
Neither og these works, I'm afraid. These are genericised classes - Map[String,Boolean] is not the same as Map[java.lang.String,java.lang.Boolean], so you get: type mismatch; found : java.lang.Object with scala.collection.jcl.MapWrapper[String,Boolean] { ... } required: Map[String,Boolean] (using the first example)
George
had time to try it. full sample provided
jitter
A: 

I think I have a partial answer...

If you convert the java map to a scala map with the java types. You can then map it to a scala map of scala types:

val javaMap = new java.util.TreeMap[java.lang.String, java.lang.Boolean]
val temp = new collection.jcl.MapWrapper[java.lang.String,java.lang.Boolean] {
    override def underlying = javaMap
}
val scalaMap = temp.map{
    case (k, v) => (k.asInstanceOf[String] -> v.asInstanceOf[Boolean])
}

The flaw in this plan is that the type of scalaMap is Iterable[(java.lang.String, Boolean)] not a map. I feel so close, can someone cleverer than me fix the last statement to make this work?!

thatismatt
+3  A: 

A Scala String is a java.lang.String but a Scala Boolean is not a java.lang.Boolean. Hence the following works:

import collection.jcl.Conversions._
import collection.mutable.{Map => MMap}
import java.util.Collections._
import java.util.{Map => JMap}

val jm: JMap[String, java.lang.Boolean] = singletonMap("HELLO", java.lang.Boolean.TRUE)

val sm: MMap[String, java.lang.Boolean] = jm //COMPILES FINE

But your problem is still the issue with the Boolean difference. You'll have to "fold" the Java map into the scala one: try again using the Scala Boolean type:

val sm: MMap[String, Boolean] = collection.mutable.Map.empty + ("WORLD" -> false)
val mm = (sm /: jm) { (s, t2) => s + (t2._1 -> t2._2.booleanValue) }

Then mm is a scala map containing the contents of the original scala map plus what was in the Java map

oxbow_lakes