David Rod
David Rod

Reputation: 11

How to fill this HashMap Map<Integer, Map<String, Boolean>> pr

I am trying to fill this map from arrays and a variable but it gives me an error , any help plz?

Map<Integer, Map<String, Boolean>> pr= new HashMap<Integer, Map<String, Boolean>>();

    String letters[] = { "A", "B" };
    int code[] = { 1 , 2 , 3 , 4 };
    Boolean res;

for (int r = 0; r < code.length; r++) {
            for (int m = 0; m < letters.length; m++) {

                res= getResult(params...);

                pr.put(code[r], ({letters[m],res});

            }
        }

the output must be this // [{1,[{"A", true}, {"B", false}]}, {...}]

Upvotes: 0

Views: 159

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

If you are using Java-9+ you can use Map.of like this:

for (int c : code) {
    for (String letter : letters) {
        res = getResult(...);
        pr.put(c, Map.of(letter, res));
    }
}

Upvotes: 1

dilanx
dilanx

Reputation: 143

You'll need to define another map with the value type of your outer map.

res = getResult(...);

Map<String, Boolean> innerMap = new HashMap<>();
innerMap.put(letters[m], res);

pr.put(code[r], innerMap);

Upvotes: 1

Related Questions