Note that there are some explanatory texts on larger screens.

plurals
  1. POJava SortedMap to Scala TreeMap
    text
    copied!<p>I'm having trouble converting a java SortedMap into a scala TreeMap. The SortedMap comes from deserialization and needs to be converted into a scala structure before being used. </p> <p>Some background, for the curious, is that the serialized structure is written through XStream and on desializing I register a converter that says anything that can be assigned to <code>SortedMap[Comparable[_],_]</code> should be given to me. So my convert method gets called and is given an <code>Object</code> that I can safely cast because I know it's of type <code>SortedMap[Comparable[_],_]</code>. That's where it gets interesting. Here's some sample code that might help explain it.</p> <pre><code>// a conversion from comparable to ordering scala&gt; implicit def comparable2ordering[A &lt;: Comparable[A]](x: A): Ordering[A] = new Ordering[A] { | def compare(x: A, y: A) = x.compareTo(y) | } comparable2ordering: [A &lt;: java.lang.Comparable[A]](x: A)Ordering[A] // jm is how I see the map in the converter. Just as an object. I know the key // is of type Comparable[_] scala&gt; val jm : Object = new java.util.TreeMap[Comparable[_], String]() jm: java.lang.Object = {} // It's safe to cast as the converter only gets called for SortedMap[Comparable[_],_] scala&gt; val b = jm.asInstanceOf[java.util.SortedMap[Comparable[_],_]] b: java.util.SortedMap[java.lang.Comparable[_], _] = {} // Now I want to convert this to a tree map scala&gt; collection.immutable.TreeMap() ++ (for(k &lt;- b.keySet) yield { (k, b.get(k)) }) &lt;console&gt;:15: error: diverging implicit expansion for type Ordering[A] starting with method Tuple9 in object Ordering collection.immutable.TreeMap() ++ (for(k &lt;- b.keySet) yield { (k, b.get(k)) }) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload