Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

843793May 13 2010 — edited May 14 2010
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?

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 11 2010
Added on May 13 2010
3 comments
172 views