0

I merged a scala Set of scala Maps using a generic function

def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] =
(Map[A, B]() /: (for (m <- ms; kv <- m) yield kv))
{
  (a, kv) =>
  a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv)
}

This handles the case when there is a clash of same keys. However, I wanted to do it with Java collections in Scala Code. I researched a bit and came across JavaConversions. I imported it and wrote this

def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] =
(new util.HashMap[A, B] /: (for (m <- ms; kv <- m) yield kv))
{
  case (a, kv) =>
    a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv)
}

However, it says there is a type mismatch

Error:(67, 11) type mismatch;
found   : scala.collection.mutable.Map[A,B]
required: java.util.HashMap[A,B]
    a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv)
      ^

Is not JavaConversions used to implicitly convert util.HashMap to mutable.Map? What am I missing here?

rrrocky
  • 696
  • 1
  • 11
  • 27

2 Answers2

7

Would a JavaConverter do what you want?

scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._

scala> val x = (new java.util.HashMap[Int,Int]).asScala
x: scala.collection.mutable.Map[Int,Int] = Map()
jwvh
  • 50,871
  • 7
  • 38
  • 64
  • @jwvh sir , can you help me with this https://stackoverflow.com/questions/53152042/how-can-i-access-a-method-which-return-option-object/53152262?noredirect=1#comment93201151_53152262 – BdEngineer Nov 05 '18 at 14:34
2

They say to try JavaConverters, as JavaConversions is deprecated.

scala> import collection.JavaConverters._
import collection.JavaConverters._

scala> def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] =
     | (new java.util.HashMap[A, B] /: (for (m <- ms; kv <- m) yield kv)) {
     | case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
<console>:16: error: value contains is not a member of java.util.HashMap[A,B]
       case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
                                  ^
<console>:16: error: java.util.HashMap[A,B] does not take parameters
       case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
                                                               ^
<console>:16: error: type mismatch;
 found   : (A, B)
 required: String
       case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
                                                                                    ^
<console>:15: error: type mismatch;
 found   : java.util.HashMap[A,B]
 required: Map[A,B]
       (new java.util.HashMap[A, B] /: (for (m <- ms; kv <- m) yield kv)) {
                                    ^

scala> def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] =
     | (new java.util.HashMap[A, B].asScala /: (for (m <- ms; kv <- m) yield kv)) {
     | case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
<console>:15: error: type mismatch;
 found   : scala.collection.mutable.Map[A,B]
 required: scala.collection.immutable.Map[A,B]
       (new java.util.HashMap[A, B].asScala /: (for (m <- ms; kv <- m) yield kv)) {
                                            ^

scala> def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] =
     | (new java.util.HashMap[A, B].asScala.toMap /: (for (m <- ms; kv <- m) yield kv)) {
     | case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
mergeMaps: [A, B](ms: Set[Map[A,B]])(f: (B, B) => B)Map[A,B]

Perhaps to show why it's deprecated:

scala> def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] =
     | (new java.util.HashMap[A, B] /: (for (m <- ms; kv <- m) yield kv)) {
     | case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
<console>:19: error: type mismatch;
 found   : scala.collection.mutable.Map[A,B]
 required: java.util.HashMap[A,B]
       case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
                         ^
<console>:18: error: type mismatch;
 found   : java.util.HashMap[A,B]
 required: Map[A,B]
       (new java.util.HashMap[A, B] /: (for (m <- ms; kv <- m) yield kv)) {
                                    ^

Noting that the for comprehension yields a set of pairs.

scala> def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B) = for (m <- ms; kv <- m) yield kv
mergeMaps: [A, B](ms: Set[Map[A,B]])(f: (B, B) => B)scala.collection.immutable.Set[(A, B)]

Apparently inference fails to both do the conversion and then figure out the op types.

Sometimes breaking apart the expression assists inference, but not here.

scala> def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] = {
     | val ss = for (m <- ms; kv <- m) yield kv
     | (new java.util.HashMap[A, B] /: ss) {
     | case (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) }
     | }
som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Deprecated? I don't find any mention on either the [docs](http://www.scala-lang.org/api/current/#scala.collection.JavaConversions$) or the [source](https://github.com/scala/scala/blob/v2.11.8/src/library/scala/collection/JavaConversions.scala#L1). – jwvh Jul 12 '16 at 06:04
  • I have the advantage in that I contributed the deprecation recently: http://www.scala-lang.org/api/2.12.0-M5/scala/collection/JavaConversions$.html – som-snytt Jul 12 '16 at 06:15
  • @jwvh I remember there's also a slight improvement in the docs, as most improvements are incremental: http://www.scala-lang.org/api/2.12.0-M5/scala/collection/JavaConverters$.html – som-snytt Jul 12 '16 at 06:17
  • Also, don't be totally blown away by the new scaladoc page style. – som-snytt Jul 12 '16 at 06:23
  • Mmm, I don't think "blown away" is how I'd describe my reaction, but I am impressed with your insight. Thanks for sharing. – jwvh Jul 12 '16 at 06:30