i have map of map i created with
Map<String, Map<String, String>> doubleMap = new HashMap<>();
and i try iterating over values of keys in second map.
so it can be like:
Key1 -> KeyA -> value1
|--------> KeyB -> value2
|--------> KeyC ->value3
Key2 -> KeyA -> value2
|--------> KeyB -> value4
|--------> KeyC -> value1
continuing on for long as i want.
i want to check if there is value in map that matches an other string.
so i have mainValue = "value2"
and i try loop over all values in doubleMap
with value variable called thisValue
and increment counter if thisValue.equals(mainValue)
i dont know how i get thisValue
. i try like this
thisValue = doubleMap.get(currentNumKey).keySet().toArray()[counter]
but that give me letterKeys.
if i do thisValue = doubleMap.get(currentNumKey).get(currentLetterKey)
it give me the right value i want, but i not can iterate over all values
thanks you
Map<String, Map<String, String>> nestedMap = new HashMap<>();
nestedMap.put ...
Renamed (doubleMap is very misleading), and indicated that creating an empty alone map wont do.
Now you can iterate your maps like:
for (Entry<Map<Map<String, String>>> outer : nestedMap.entrySet) {
outer.getKey() ... would be "Key1"
outer.getValue() ... represents that inner map
And for the inner loop, it is very much the same; meaning that you can use for-each and entrySet() on the value returned by outer.getValue().
You can obtain a Collection
of all the values in a map using the values()
method. For doubleMap
, that will give you a collection of Map<String,String>
. You can then iterate over that collection and for each Map
, do the same trick to find your value:
for (Map<String,String> map : doubleMap.values()) {
for (String val : map.values()) {
if (mainValue.equals(val)) {
// value was found
}
}
}
If you need the keys that lead to that value (which, of course, may occur more than once), then you'll need to iterate over the key set, obtained using Map.keySet()
(or entry set, obtained using Map.entrySet()
) for doubleMap
and also for each Map
retrieved for each key. The loops should look similar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.