我的代碼導致java.util.ConcurrentModificationException錯誤。這是下面的代碼。如何重新設計方法才能正常工作
這裏是確切的錯誤:
java.util.ConcurrentModificationException
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:894)
at java.util.HashMap$KeyIterator.next(HashMap.java:928)
at ca.on.oicr.pinery.lims.gsle.GsleClient.getOrders(GsleClient.java:720)
720線是第二個for循環
我之前張貼了這個問題,被告知「您要添加到訂單環多數民衆贊成遍歷內訂單的元素,這就是導致異常的原因,不要修改你在循環中循環的集合,同樣對樣本「。我明白我要重新構建這個方法並收到以下建議。
ListIterator<Order> it = orders.listIterator();
while (it.hasNext()) {
Order ord = it.next();
if () // some condition
it.remove(); // This wil remove the element that we just got using the next() method
if () // some other condition
it.add(new Order()); // THis inserts the element immediately before the next call to next()
}
現在我停留在如何做樣品並利用迭代方法時,因爲你雖然一組迭代不同訂單的增加,我想我會用一個for循環。
這是我越來越困惑如何改變的部分,以便不會得到java.util.ConcurrentModificationException。
f
到目前爲止,我已經到了這裏。
java.util.ListIterator<Order> it = orders.listIterator();
while (it.hasNext()) {
it.next().getId();
if (sampleOrderMap.containsKey((it.next().getId())))
{
Set<OrderSample> samples = sampleOrderMap.get(it.next().getId());
}
}`
我只是不知道如何把在其他的方式,我不會讓ConcurrentModificationException的
您可以將一些臨時集合添加到您正在迭代的其他集合中。 然後,在完成迭代之後,將新的臨時集合添加到原始集合中。 –
此問題需要標題更改。不完全確定要做什麼。 – Cruncher