   [java] ConcurrentModificationException
  I've seen this has been posted before, but I still don't really know what to do to fix this problem that occur.Basicly, what I have is a Bank program. You can create customers, and within every customer you can create savingsaccounts and creditaccounts. Within every account you can add funds, remove funds, and delete account, which if chosen the current account will be deleted and the interest will be shown.Almost the same thing applies to when deleting a customer. Then it should iterate through each account, and delete them after getting and storing the interest they return.However, in the iteration this exception is thrown.The code looks like this:public String deleteAccounts(){SavingsAccount currentSAccount;CreditAccount currentCAccount;String accounts = "";double interest = 0.0;this.totalInterest = 0.0;Map sAccounts = this.customer.getSavingsAccounts();Map cAccounts = this.customer.getCreditAccounts();Map.Entry account;    Iterator allSavingsAccounts = sAccounts.entrySet().iterator();    Iterator allCreditAccounts = cAccounts.entrySet().iterator();   while(allSavingsAccounts.hasNext())    {   account = (Map.Entry) allSavingsAccounts.next();    currentSAccount = (SavingsAccount) account.getValue();    interest = currentSAccount.prepareDelete();   if(accounts != "") accounts += ", ";   accounts += currentSAccount.getAccountNumber() + ": " + interest;    this.totalInterest += interest;   this.customer.removeAccount(currentSAccount.getAccountNumber());   }   while(allCreditAccounts.hasNext())    {   account = (Map.Entry) allCreditAccounts.next();    currentCAccount = (CreditAccount) account.getValue();    interest = currentCAccount.prepareDelete();   if(accounts != "") accounts += ", ";   accounts += currentCAccount.getAccountNumber() + ": " + interest;    this.totalInterest += interest;   this.customer.removeAccount(currentCAccount.getAccountNumber());   }   return accounts;}This code works great if the customer only has 1 account of each type, but if the iteration has to be done more than once, this line gives an error:account = (Map.Entry) allSavingsAccounts.next();oraccount = (Map.Entry) allCreditAccounts.next();respectivly, depending on which accounttype it has to iterate more than once.Does anyone know how to solve this?I read I have to use something like "syncronize" or something, how would I implement that?Thanks a bunch![Edited by - Tanax on May 11, 2009 3:48:52 PM]
  I suspect that this has nothing to do with different threads.  The problem is, depending on the type of collection you are using, Java might not let you remove items from a collection while you are iterating through the collection.What you need to do is figure out a way to remove the closed accounts without looping the list of them.Something like this...Vector<Item> toDelete = new Vector<Item>();for( Item item : collection ) {  if( item.shouldDelete() )toDelete.add( item );}// now remove the items...for( Item item : toDelete ) {  collection.remove( item );}P.S. - Take a look at the FAQ and use the source tags to format source code.;  About sourcetags, sorry! I've updated the mainpost now, thanks for tip!The thing about the code is that I'm not actually removing anything from the list that I'm iterating through. The list I'm iterating through is essentially containing the elements that should be removed from the customer class.The getSavingsAccounts() returns a map of all accounts that are stored in the customer. I'm iterating that list, and remove the accounts(all accounts) from the customer.Here's the customer class, maybe it will help clear some things up:import java.util.*;public class Customer {private String name;private long socialSecurityNumber;private Map sAccounts;private Map cAccounts;    public Customer(String customerName, long number) {   this.name = customerName;    this.socialSecurityNumber = number;   this.sAccounts = new TreeMap();    this.cAccounts = new TreeMap();   }    public int createNewAccount(int type)    {   int number = this.generateNumber();    if(this.sAccounts.containsKey(number) || this.cAccounts.containsKey(number))    {   this.createNewAccount(type);   }   else    {   if(type == 1)    {   SavingsAccount account = new SavingsAccount(number);    this.sAccounts.put(number, account);   }   else    {   CreditAccount account = new CreditAccount(number);    this.cAccounts.put(number, account);   }  }   return number;   }    private int generateNumber()    {   double random = Math.random()*1000;    double random2 = Math.random()*1000;    double random3 = Math.random()*1000;   double randomize = random * random2 + random3;    int generated = (int) randomize;   return generated;   }    public String getCustomerName()    {   return this.name;   }  public long getCustomerNumber()    {   return this.socialSecurityNumber;   }   public void setName(String name)    {   this.name = name;   }public int getAccountType(int number){if(this.sAccounts.containsKey(number)){return 1;}else if(this.cAccounts.containsKey(number)){return 2;}return 0;}   public SavingsAccount getSavingsAccount(int number)    {   SavingsAccount object;    object = (SavingsAccount) this.sAccounts.get(number);   return object;   }   public CreditAccount getCreditAccount(int number)    {   CreditAccount object;    object = (CreditAccount) this.cAccounts.get(number);   return object;   }   public Map getSavingsAccounts()    {   return this.sAccounts;   }   public Map getCreditAccounts()    {   return this.cAccounts;   }   public int getCreditAccountsLength()    {   return this.cAccounts.size();   }   public int getSavingsAccountsLength()    {   return this.sAccounts.size();   }   public Boolean removeAccount(int number)    {   if(this.sAccounts.containsKey(number))    {   this.sAccounts.remove(number);    return true;   }   else if(this.cAccounts.containsKey(number))    {   this.cAccounts.remove(number);    return true;   }   return false;   }    };Quote:Original post by TanaxThe thing about the code is that I'm not actually removing anything from the list that I'm iterating through.You will get this exception when you call iterator.next() if the data structure you got the iterator from, a map in this case, has been altered since you got the iterator.// Here is the MapMap sAccounts = this.customer.getSavingsAccounts();// This is the iterator to the mapIterator allSavingsAccounts = sAccounts.entrySet().iterator();    while(allSavingsAccounts.hasNext()){  // This will throw an Exception if the map is altered account = (Map.Entry) allSavingsAccounts.next();  // code removed // Even though this is in another class, it still alters the map  this.customer.removeAccount(currentSAccount.getAccountNumber());    }Java doesn't care what collection you are looping through.  If you call the iterator.next() method on a collection after you remove something from the collection, you will get an exception.  Does that make sense?;  Oh! I understand! I was under the believe that when I create Map sAccounts, that would basicly be a NEW variable containing the values from this.sAccounts from customer class, and then when I altered the customer class this.sAccounts it wouldn't affect Map sAccounts. Because that's how it works in for example PHP. Apparently it's not the same in Java.So, exactly how would I solve this now again? Sorry for all of these questions!And thanks for all your help.   ;  You need sAccounts be a completely independent map?-> Map sAccounts = new HashMap(customer.getSavingsAccounts());   ;  Gee, that worked excellent! Now it's working :) Thanks alot!I suppose I could've done it Glass_Knife's way aswell with adding each accountnumber that should be deleted into a seperate array, and then loop through it and delete from the customer. I found this solution easier though. And it's working, so.. thanks :)   