현재 코드는 for-each Java 5 루프를 사용하여 ConcurrentHashMap을 반복합니다. 아래의 코드에서 암시 적 반복자는 오류 방지이므로 ConcurrentModificationException을 실행하지 않습니다.ConcurrentHashmap 반복하기
Map<Long,String> map = new ConcurrentHashMap <Long,String>();
map.put(1L, "1");
map.put(2L, "2");
System.out.println("map size before" + map.size());
for (Long id : map.keySet()) {
map.remove(id);
}
System.out.println("map size after" + map.size());
내가는, Iterator.remove()
코드가 이미 제작 중이며 불행히도 변경할 수 없습니다. 내가 반복자 패턴을 사용했으면 좋겠다. – Noosphere