0

I have data like this {"Atr1":{"col":1,"row":1}, "Atr2":{"col":1,"row":2}, ...}. the columns and rows are always fixed, so I want to create final attribute in java.
How to initialize Map<String, Map<String, Integer>> and the values.

I have tried the following code, is there a better way?

private final Map<String, HashMap<String, Integer>> FIELD_TO_INDEX = new HashMap<String, HashMap<String, Integer>>() {
        private static final long serialVersionUID = 1L;
        {
            put("Atr1",
                    new HashMap<String,Integer>(){
                        private static final long serialVersionUID = 1L;
                        {
                            put("col",1);
                            put("row",1);
                        }
                    }
            );
            put("Atr2",
                    new HashMap<String,Integer>(){
                private static final long serialVersionUID = 1L;
                {
                    put("col",1);
                    put("row",2);
                }
            }
                    );
            put("Atr3",
                    new HashMap<String,Integer>(){
                private static final long serialVersionUID = 1L;
                {
                    put("col",1);
                    put("row",3);
                }
            }
                    );
        

}
    };
 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

1

If you don't mind it immutable, maybe

Map<String, Map<String, Integer>> outer = Map.of(
    "Map1", Map.of(
        "InnerMap1", 1,
        "InnerMap2", 2
        )
    );
g00se
  • 3,207
  • 2
  • 5
  • 9