mercredi 4 mars 2015

How do I return a defined Map?



I want to define a function which returns an instantiated map depending on the Key and Value class which I give it:


something like



Map<Integer, String> map = getMap(Integer.class, String.class);


but I am unsure how to define the getMap function in the correct way. I was thinking something like



<K,V> Map<K,V> getMap(Class<K> keyClass, Class<V> valClass) {
Map<keyClass, valClass> retMap = new HashMap<keyClass, valClass);


.... // add values to my map

return retMap;
}


But this doesn't seem to work at all.


does anyone have any suggestions?




Aucun commentaire:

Enregistrer un commentaire