I know there are several ways to iterate through a hashmap, but what is a good way to modify a hashmap as you go along (other than just creating a new hashmap and getting rid of the old one)
I want something like
for (Map.Entry<String, Integer> entry : wordcounts.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
if(blacklist.contains(key))
//remove key/value for that key from wordcounts
if(mappings.contains(key))
//change key in wordcounts from one string to another based on the key's value in a <string,string> map (mappings)
}
Will it be possibly for me to modify my map while I'm going through it? Do i have to use an iterator?
Iteratorto remove entries as you come upon them. Changing the key value is going to require creating a newMapand either copying the unchanging values or modifying and inserting the changing ones. The cost in time and memory isn't too large if you remove them from the oldMapas you add to the new one.