Reputation: 11
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
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
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