3 Replies Latest reply on May 14, 2010 8:16 PM by 843793

    Returning a HashMap<K, M> from a method of a class typed<M>

      So, this is what I have. I have a class parameterized for a bean type of a collection it receives, and in a method of the class, I need (would like) to return a HashMap typed as <K, M> as in:
      public class MyClass<M> {
         private Collection<M> beanList;
         public HashMap<K, M> getMap(  /* what do I do here */ );
      I tried to cheat from looking at the Collections.sort method, knowing it uses the List type in its return type as in:
      public static <T extends Comparable<? super T>> void sort(List<T> list)
      So, I am trying:
      public HashMap<K, M> getMap(Class<K> keyType)
      But, it is telling me I need to create class K.

      So... how does the Collections.sort method get away with it and I can't????

      I suppose I could write the method as:
      public HashMap<Object, M> getMap()
      but became somewhat challenged by my first guess.

      Any ideas... or just go with the HashMap with the untyped key?