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?

2 Answers 2

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

1 Comment

@jwvh sir , can you help me with this stackoverflow.com/questions/53152042/…
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) }
     | }

5 Comments

Deprecated? I don't find any mention on either the docs or the source.
I have the advantage in that I contributed the deprecation recently: scala-lang.org/api/2.12.0-M5/scala/collection/…
@jwvh I remember there's also a slight improvement in the docs, as most improvements are incremental: scala-lang.org/api/2.12.0-M5/scala/collection/…
Also, don't be totally blown away by the new scaladoc page style.
Mmm, I don't think "blown away" is how I'd describe my reaction, but I am impressed with your insight. Thanks for sharing.

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.