기타/에러

[에러/Java] ConcurrentModificationException

hrming 2023. 2. 2. 16:30

▶ 발생 에러 : ConcurrentModificationException

 에러 원인 : 컬렉션을 순회하면서 순회하는 대상 컬렉션에 수정을 가하는 경우

 해결 방법 : 컬렉션에 직접 수정하지 않고, Iterator 사용 

Map을 .keySet()을 사용하여 Set으로 변경 후, String에 담아서 for문을 돌리면서 Map에서 remove를 하니 위와 같은 문제가 발생함. String을 Iterator로 변경 하고 for문을 돌리고, iterator를 remove 처리하니 해결됨 !-!

void java.util.Iterator.remove()
Removes from the underlying collection the last element returned by this iterator (optional operation). 

This method can be called only once per call to next.

 

 

참고/출처: https://hbase.tistory.com/322

 

[Java] ConcurrentModificationException 원인과 해결방법

자바 언어로 프로그래밍을 하다보면 가끔씩 ConcurrentModificationException을 만나게 된다. ConcurrentModificationException의 발생 원인과 해결방법에 대해서 간단하게 소개하겠다. ConcurrentModificationException Conc

hbase.tistory.com