title: Using Java generic class with raw types in Scala
body: <p>I want to use a Java library in my Scala program. The library contains a generic class which is part of other classes:</p>
  
  <pre><code>package java.items;
  
  public class Item&lt;T extends Comparable&gt; implements Comparable&lt;Item&gt; {  
    private T id;
   ...
  }
  
  public final class Itemset{
    private List&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
    public List&lt;Item&gt; getItems() { return items; }
   ...
  }
  
  public class Sequence {
    private final List&lt;Itemset&gt; itemsets = new ArrayList&lt;Itemset&gt;();
    public List&lt;Itemset&gt; getItemsets() { return itemsets; }
   ...
  }
  </code></pre>
  
  <p>In my Scala code, I loop over the different objects and need to instantiate a hashmap of type <code>[T, Int]</code> to store the Ids with a counter:</p>
  
  <pre><code>import java.items._
  
  object ConvertSequence {
  
    def ConvertSequence (dataset: RDD[(Sequence)], sc: SparkContext) {
  
      sc.broadcast(dataset.flatMap(r =&gt; {
        val itemCounts = new HashMap[AnyRef, Int]
  
        for (itemset &lt;- r.getItemsets) {
          for (item &lt;- itemset.getItems) {
            val i = itemCounts.getOrElse(item.getId, 0)
            itemCounts.update(item.getId, i + 1)
          }
        }
        itemCounts
      }).
      map(r =&gt; (r._1, (r._2, 1))).
      reduceByKey((x, y) =&gt; (x._1 + y._1, x._2 + y._2)).
      sortBy(r =&gt; (r._2._1, r._1))
      zipWithIndex().
      collect({ case (k, v) =&gt; (k._1, v)})
    )
  }
  </code></pre>
  
  <p>I don't know which type to pass to the hashmap constructor (<code>T</code> is not available from my Scala object as only <code>Item</code> is typed).
  I've tried <code>AnyRef</code> but I get a error at compilation:</p>
  
  <pre><code>[ERROR]  error: type mismatch;
  [INFO]  found   : ?0
  [INFO]  required: AnyRef
  [INFO] Note that ?0 is unbounded, which means AnyRef is not a known parent.
  [INFO] Such types can participate in value classes, but instances
  [INFO] cannot appear in singleton types or in reference comparisons.
  [INFO]           val i = itemCounts.getOrElse(item.getId, 0)
  [INFO]                                             ^
  [ERROR] one error found
  </code></pre>
  
  <p>How can I manage polymorphism between my Java and Scala code?</p>