tôi có mã này dưới đây, và tôi nhận được một ConcurrentModificationException bằng cách thực hiện dòng sau:ConcurrentModificationException trong bộ sưu tập unmodifiable
filterCardsToDevice(getCollection());
mã:
private List<MyClass> filterCardsToDevice(Collection<MyClass> col) {
final List<MyClass> newList = new ArrayList<MyClass>();
for (MyClass myObj : col) {
long id = myObj.getId();
if (id < 0 || id > 0xFFFFFFFFl) {
// just a log here
} else {
newList.add(myObj);
}
}
return newList;
}
private final Map<Long, MyClass> map = new HashMap<Long, MyClass>();
public Collection<MyClass> getCollection() {
synchronized (map) {
return Collections.unmodifiableCollection(map.values());
}
}
Stack là:
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:841)
at java.util.HashMap$ValueIterator.next(HashMap.java:871)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1010)
Chính xác trên đường truyền trực tuyến:
for (MyClass myObj : col) {
Tôi không hiểu tại sao lỗi này xảy ra, bởi vì tôi không sửa đổi danh sách.