Comment puis-je créer vivify une clé avec des génériques? Ce code ne compile même pas:Fonction autovivify générique pour les cartes
/* populate the map with a new value if the key is not in the map */
private <K,V> boolean autoVivify(Map<K,V> map, K key)
{
if (! map.containsKey(key))
{
map.put(key, new V());
return false;
}
return true;
}
Cela semble compiler (non testé): '(V) (map.getClass(). GetTypeParameters() [1] .getClass()). NewInstance()' – perreal