title: Generic Map with Lists of Comparable
body: <p>I want a <code>Map</code> with several lists, each of them composed of <code>Comparable</code> objects, but not always the same, so there might be a <code>List</code> of <code>Doubles</code> and a <code>List</code> of <code>Strings</code>, etc...</p>
  
  <p><code>Map&lt;String, List&lt;Comparable&lt;?&gt;&gt;&gt;</code></p>
  
  <p>Unfortunately the <code>Map</code> as defined above will not be useful because the elements in each <code>List</code> can't be compared. Therefore I had to cheat and introduce a type parameter to the class:</p>
  
  <pre><code>class MyClass&lt;T extends Comparable&lt;? super T&gt;&gt; {
      Map&lt;String, List&lt;T&gt;&gt; myMap;
  }
  </code></pre>
  
  <p>This is not totally correct because not all lists are of the same type. But it works, the only thing I have to do is to do a type cast to <code>T</code> when I add a <code>Double</code> to a list of doubles or a <code>String</code> to a list of strings. I make sure that each type gets added only to lists of the correct type.</p>
  
  <p>Is there a better way to solve this problem?</p>
  
  <p>The alternative is to have one <code>Map</code> for every type of <code>List</code>. But this just would make the code uglier and the only thing I do with the lists is to sort them and insert them into a DB as String values, therefore I only call <code>toString</code> on the comparables.</p>