下面的代码试图利用HashMap的Iterator对象遍历该HashMap并删除满足条件的元素(比如超时的元素),但会抛出java.util.ConcurrentModificationEx
}
由于删除元素时,hs的iterator对象也重新排序,所以只要用hs的一个副本hsBack
Uackp的iterator去遍历hs即可,这样在删除hs元素时iterator就不会重排了(因为删除的是hs的元素,而不是该iterator所属的hsBackUackp)
...................
.....................
void remove() Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method. 于是有下面的改进: 解决方案三: .............................. Iterator it=hs.keySet().iterator(); while(it.hasNext()) { String str=(String)it.next(); System.out.println(hs); it.remove(); } ..............................
1225



被折叠的 条评论
为什么被折叠?



