2

I have the following data structure:

java.util.Map[List[String],List[String]] = {[10, 20]=[1500], [5, 7]=[1400]}

I am trying to extract the numbers 10 20 5 and 7 using Scala. The way I was looking to achieve this is:

map.head._1 -> to extract 10 (map.head returns a tuple)
map.head._2 -> to extract 20 (map.head returns a tuple)

However, I am getting the following exception:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to scala.collection.immutable.List

I have read about importing import scala.collection.JavaConversions._ however, this did not fix anything.

Thanks, any help is highly appreciated!

The piece of code that tries to achieve this is:

 def getTokenRangeForKeys(params: String): java.util.Map[List[String], List[String]] = {
    invokeOperation[java.util.Map[List[String], List[String]]]("XXX", "YYY", Array(params))
  }

The above method returns my map, which looks like this:

  map = java.util.Map[List[String],List[String]] = {[10, 20]=[1500], [5, 7]=[1400]}

What I have tried so far:

map.head._1 -> java.lang.ClassCastException: java.util.ArrayList cannot be cast to scala.collection.immutable.List

scalaMap = map.asScala
m.headOption match {
  case Some((h1, h2)) => println((h1, h2)) -> java.util.ArrayList cannot be cast to scala.collection.immutable.List
  case None => ...
}
4
  • Can you show how you're declaring the map? Commented Jan 11, 2017 at 9:27
  • Show your code - post a complete piece of code that reproduces the problem, not just a single statement snippet (like map.head._1 etc.), that will make it much easier to tell what exactly is wrong. Commented Jan 11, 2017 at 9:28
  • This is my entire code related to this. A method returns a java Map that needs to be worked with in a Scala script. Commented Jan 11, 2017 at 9:38
  • Edit: I've posted the said code Commented Jan 11, 2017 at 9:51

2 Answers 2

3

I think your declaration of what comes from Java world should be:

java.util.Map[java.util.List[String], java.util.List[String]]

In the current form of java.util.Map[List[String], List[String]] you declare a java Map of Scala Lists, which is probably not what you want. JVM is not complaining when you pass your Java types because only top level type is checked as a part of function signature check - this is called type erasure.

On this you should use JavaConverters asScala to convert to corresponding Scala types as written in the Reactormonk answer:

import scala.collection.JavaConverters._
val m = map.asScala.map{case (h, k) => (h.asScala, k.asScala)}
Sign up to request clarification or add additional context in comments.

1 Comment

This is indeed a solution. It works as expected now!
0

Don't use JavaConversions.

import scala.collection.JavaConverters._
val m = map.asScala.map({case (h, k) => (h.asScala, k.asScala)})
m.headOption match {
  case Some((h1, h2)) => ...
  case None => ...
}

8 Comments

I see what you are trying to do, but I can't get the syntax write I guess. It's my first ever scala project, could you please provide the additional code that achieves this?
On the pattern matching, I'm using headOption so you handle the case of an empty Map.
I get back the same exception. I tried to just print (h1, h2) like this: ... => println((h1, h2))
h.asScala and k.asScala throw an error: value asScala is not a memeber of List[String]
map.getClass() -> Class[_ <: java.util.Map[List[String],List[String]]] = class java.util.HashMap
|

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.