jeudi 26 mai 2016

Getting map item with reflection

I'm trying to get a map value by its key using reflection:

HashMap<String, Float> myMap = new HashMap<String, Float>();
myMap.put("time", Float.valueOf(-1.7));
String param = "time";
//...
float modif = (float)myMap.getClass().getDeclaredMethod("get", String.class).invoke(myMap, param);

But I get this error notification:

java.lang.NoSuchMethodException: java.util.HashMap.get(java.lang.String)

Can anybody, please, suggest why?





Aucun commentaire:

Enregistrer un commentaire