Retrieving variables with the same name as a string

I have a bunch of maps inside a class and I would like to make a generic method to retreive the specific map that matches the ID.
Something like:

private Map map1;
private Map map2;
private Map map3;

public Map getMap(String id){
  return mapX;

And when I call the method as:


It will return map1.
Any way I can do this conversion whithout creating a map of maps?

Try to use reflection, for example:

    private Map getMap(String id) throws NoSuchFieldException, IllegalAccessException {
        getClass().getField("map" + id).get(this);
        return map;

define a Map of String, Map and use it as dictionary


private Map map1;
private Map map2;
private Map map3;
private Map<String, Map> mapX;

public Map getMap(String id) {
    return this.mapX.getOrDefault(id, null);

If you are having only few numbers of map then you can use the switch statement and retuen the map which matches the map name like below code :-

switch (mapName) {
            case "map1":
                return map1;
            case "map2":
                return map2;

But if you have large number of Maps then its better to give some id or name to them and use Map of Map.

Map of map can be also the solution for this :

Map<String, Map<String, String>> map = new HashMap<>();

    map.put("map1", map1); 
    map.put("map2", map2);

public static Map<String, String> getMap(String name){
   return map.get(name);