2

The below class causes the compiler error :

value toMap is not a member of 
 java.util.Map[java.lang.String,java.util.List[com.recommendations.TestObject]]


class ProcessData(var distanceMap : java.util.Map[java.lang.String , java.util.List[TestObject]]) {

    def apply = {

      val m2: Map[String, Any] = distanceMap.toMap

    }

}

This line causes the error :

val m2: Map[String, Any] = distanceMap.toMap

Is the error because the value of Map distanceMap is an mutable java.util.List , hence these values need also be immutable ?

How can I build a Scala immutable map from the mutable distanceMap above ?

1

2 Answers 2

3
import scala.collection.JavaConversions._

val m = new java.util.HashMap[String, Object]()
m.put("Foo", java.lang.Boolean.TRUE)
m.put("Bar", java.lang.Integer.valueOf(1))

val m2: Map[String, Any] = m.toMap
println(m2);
Sign up to request clarification or add additional context in comments.

2 Comments

your code does not compile, java.util.Map is an interface, not a concrete class so cannot instantiate. .toMap method is not a member of java.util.Map
I'm receiving a compiler error for above code : "Multiple markers at this line - value toMap is not a member of java.util.HashMap[String,java.lang.Object] - Cannot prove that Char <:< (T, U). - Implicit conversions found: m => augmentString(m) - not enough arguments for method toMap: (implicit ev: <:<[Char,(T, U)])scala.collection.immutable.Map[T,U]. Unspecified value parameter ev."
1

You are simply missing the JavaConversions import. Your code will compile with:

import scala.collection.JavaConversions._
class ProcessData(var distanceMap : java.util.Map[java.lang.String , java.util.List[TestObject]]) {
    def apply = {
      val m2: Map[String, Any] = distanceMap.toMap
    }
}

Comments

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.