budi
budi

Reputation: 6551

Java Clear Map of Map

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

Answers (1)

Cody Woolsey
Cody Woolsey

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

Related Questions