java - Retrieving variables with the same name as a string -


i have bunch of maps inside class , make generic method retreive specific map matches id. like:

private map map1; private map map2; private map map3;   public map getmap(string id){   return mapx; } 

and when call method as:

getmap("map1"); 

it return map1. way can conversion whithout creating map of maps?

try use reflection, example:

    private map getmap(string id) throws nosuchfieldexception, illegalaccessexception {         getclass().getfield("map" + id).get(this);         return map;     } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -