A Trap When Iterate on HashMap.entrySet() Jan 25th, 2013 If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. Wrong code: 1 2 3 4 5 for (Entry<String, Long> entry : sizeMap.entrySet()) { if (xxx=xxx) { sizeMap.remove(entry.getKey());//这里删除会造成下载迭代时抛出java.util.ConcurrentModificationException异常 } } Right code: 1 2 3 4 5 6 for (Iterator<Entry<String, Long>> itr = sizeMap.entrySet().iterator();itr.hasNext();) { Entry<String, Long> entry = itr.next(); if(xxx=xxx) { itr.remove(); } }