I need to iterate over an object set and find all equal cost-categories and join them together. I tried to put a while-loop into another while-loop. However this doesn't work; I always get the ConcurrentModificationException. Why do I still get this?
How to make this work, so that it works just like two nested for-loops
public Set<ExpenseItem> getConsolidatedExpenseitems(String expenseUid) {
Expense expense = getByUid(expenseUid);
Set<ExpenseItem> expenseItems = expense.getExpenseItems();
Set<ExpenseItem> expenseItemsInner = expense.getExpenseItems();
for(ExpenseItem e : expenseItems) { // ConcurrentModificationException
Iterator<ExpenseItem> expenseItemsIteratorInner = expenseItems.iterator();
expenseItemsIteratorInner = expenseItemsInner.iterator();
while(expenseItemsIteratorInner.hasNext()) {
ExpenseItem eInner = expenseItemsIteratorInner.next();
if(e != null) {
if(eInner.getCostCategory().equals(e.getCostCategory())) {
System.out.println(e.getCalculatedAmount());
System.out.println(eInner.getCalculatedAmount());
System.out.println("---");
expenseItemsIteratorInner.remove();
}
}
}
}
return null;
}