Reputation: 6551
What is the most appropriate way to clear
a Map
of a Map
, assuming I simply want to clear all entries in the top-level Map
?
Map<String, Map<Integer, Integer>> nestedMap;
Method A: Clear the top-level map only.
nestedMap.clear();
Method B: Clear the inner maps, then clear the top-level map.
for (Map<Integer, Integer> innerMap: nestedMap.values()) {
innerMap.clear();
}
nestedMap.clear();
Upvotes: 0
Views: 322
Reputation: 131
Clearing just the outer map would be fine, unless you wanted to retain the empty inner maps for later use. By clearing the external map, the internal maps should be garbage collected.
Upvotes: 2