When you use an "enhanced" for loop such as this:
for (Map.Entry<String, String> pair : map.entrySet()){
There is really an implicit Iterator behind the scenes that gives you a pair on each iteration. Section 14.14.2 of the JLS states:
The enhanced for statement is equivalent to a basic for statement of the form:
for (I #i = Expression.iterator(); #i.hasNext(); ) {
VariableModifiersopt TargetType Identifier =
(TargetType) #i.next();
Statement
}
The Iterator will throw a ConcurrentModificationException if anything modifies the collection while it's iterating it, unless you call the iterator's own remove() method, which removes the current element. You'll have to use an explicit Iterator:
Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
while (itr.hasNext())
{
Map.Entry<String, String> pair = itr.next();
if (yourCriteriaIsMet)
{
itr.remove();
}
}